Hi,
I am using apache commons HttpClient for sending SOAP request to one of my vendor.
Till now I used the HttpClient 2.0. it was working fine.
When I update the HttpClient3.0, I am always getting response code 500 (server error) from vendor.
Can any one help me What is wrong in below 3.0 code snippet?
I am suspecting my ESP authentication part is not correct ....
HttpClient 3.0 code:
static {
// add security providers
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
try {
ACCURINT_URL = new URL("https://myvendor.com/");
HOST_CONFIG = new HostConfiguration();
HOST_CONFIG.setHost(ACCURINT_URL.getHost(), ACCURINT_URL.getPort(), ACCURINT_URL.getProtocol());
}
catch (Exception ex) {
logger.error("Unable to initilize ACCURINT url");
}
try{
String secProviderName = "com.sun.crypto.provider.SunJCE";
java.security.Provider secProvider =
(java.security.Provider)Class.forName(secProviderName).newInstance();
Security.addProvider(secProvider);
}catch(Exception ex1){
logger.error("Unable to initilize security provider");
}
}
public String execute(String _request) {
HttpClient client = new HttpClient();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("username", "password");
AuthScope ascope = new AuthScope("https://myvendor.com", 443, "ESP");
client.getState().setCredentials(ascope, credentials);
HttpConnectionManagerParams cmngr = new HttpConnectionManagerParams();
cmngr.setConnectionTimeout(20000);
client.getHttpConnectionManager().setParams(cmngr);
HttpMethodParams mparam = new HttpMethodParams();
mparam.setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
PostMethod post = new PostMethod(ACCURINT_URL.getPath());
post.setParams(mparam);
post.setDoAuthentication(true);
post.addRequestHeader("Content-Type", "text/xml; charset=utf-8");
post.addRequestHeader("SOAPAction", "/MYSOAPACTION");
try {
int code = client.executeMethod(HOST_CONFIG, post);
logger.info("return code= "+code);
//logger.info("re="+post.getResponseBodyAsString());
//return post.getResponseBodyAsString();
//return post.getResponseBodyAsStream();
String XMLresponse = "";
InputStream in = post.getResponseBodyAsStream();
if(in != null){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
bos.write(buf, 0, len);
}
byte[] data = bos.toByteArray();
XMLresponse = new String(data);
//logger.info("Response= "+XMLresponse);
}
//return new AccurintReportResponse(XMLresponse,true);
return XMLresponse;
}
catch (IOException ioex) {
logger.error("Unable to get response from " + URL.getHost(), ioex);
}
finally {
post.releaseConnection();
}
return null;
}
HttpClient 2.0 code:
public String execute(String _request) {
HttpClient client = new HttpClient();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("username", "password");
client.getState().setCredentials("ESP", credentials);
client.setConnectionTimeout(20000);
PostMethod post = new PostMethod(ACCURINT_URL.getPath());
post.setUseExpectHeader(false);
post.setDoAuthentication(true);
post.addRequestHeader("Content-Type", "text/xml; charset=utf-8");
post.addRequestHeader("SOAPAction", "MYSOAPACTION");
String xml = _request.toString();
post.setRequestBody(xml);
try {
int code = client.executeMethod(HOST_CONFIG, post);
logger.debug("return code= "+code);
//logger.info("re="+post.getResponseBodyAsString());
//return post.getResponseBodyAsString();
//return post.getResponseBodyAsStream();
String XMLresponse = null;
InputStream in = post.getResponseBodyAsStream();
if(in != null){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
bos.write(buf, 0, len);
}
byte[] data = bos.toByteArray();
XMLresponse = new String(data);
logger.debug("Response= "+XMLresponse);
}
return XMLresponse;
}
catch (IOException ioex) {
logger.error("Unable to get response from " + ACCURINT_URL.getHost(), ioex);
}
finally {
post.releaseConnection();
}
return null;
}