OutBound Https call with apache DefaultHttpClient

HttpCall through Apache DeafultHttpClient, using basic authentication  in Websphere commerce, using JSON obejct in request. 

public HttpResponse getResponse() throws ECException{
  HttpResponse response = null;
DefaultHttpClient http = new DefaultHttpClient();
HttpPost post = new HttpPost("url");
String uid = "Uid";
String pwd = "Pwd";

// nc_crypt can be used to decrypt the passworde if it is encrypted and stored in DB or files

UsernamePasswordCredentials creds = new UsernamePasswordCredentials(uid , nc_crypt.decrypt(Pwd, null));
post.addHeader( BasicScheme.authenticate(creds,"UTF-8",false) );
StringEntity entity;
try {
entity = new StringEntity( //JSON Request in String form// );
entity.setContentType("application/json");
post.setEntity( entity );

// wirte a method setUpScheme for Https and it contains the code to retrieve the SSL connection    //factory settings

Scheme scheme = new Scheme("https", 443, retrieveWebsphereSSLConnectionFactory("url"));
http.getConnectionManager().getSchemeRegistry().register(scheme);
response = http.execute( post );
} catch (UnsupportedEncodingException e) {
                // write logger or throw exception
logger.error("problems while connecting ");
} catch (ClientProtocolException e) {
logger.error("problems while connecting ");
} catch (IOException e) {
logger.error("problems while connecting ");
} catch (SSLException e) {
logger.error("problems while connecting ");
} catch (URISyntaxException e) {
logger.error("problems while connecting ");
}
    return response;
}
 _________________________________________________________________________



No comments:

Post a Comment