Created
November 29, 2012 19:36
-
-
Save berlinbrown/4171310 to your computer and use it in GitHub Desktop.
POST xml BODY WITH Apache Http client libraries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Sending a HTTP post BODY with java | |
* Uses apache http client libraries. | |
* | |
* Berlin Brown | |
*/ | |
import java.io.InputStream; | |
import java.io.Serializable; | |
import java.security.KeyStore; | |
import java.security.cert.X509Certificate; | |
import java.util.Date; | |
import javax.net.ssl.KeyManager; | |
import javax.net.ssl.KeyManagerFactory; | |
import javax.net.ssl.TrustManager; | |
import javax.net.ssl.TrustManagerFactory; | |
import javax.net.ssl.X509TrustManager; | |
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; | |
import org.apache.commons.httpclient.HttpClient; | |
import org.apache.commons.httpclient.HttpStatus; | |
import org.apache.commons.httpclient.UsernamePasswordCredentials; | |
import org.apache.commons.httpclient.auth.AuthScope; | |
import org.apache.commons.httpclient.methods.PostMethod; | |
import org.apache.commons.httpclient.methods.StringRequestEntity; | |
import org.apache.commons.httpclient.params.HttpMethodParams; | |
import org.apache.commons.httpclient.protocol.Protocol; | |
import org.apache.log4j.Logger; | |
/** | |
* Tested with Apache http client 3.1 | |
*/ | |
public class ThirdParty implements Serializable { | |
private static final long serialVersionUID = 1L; | |
private String soapAction = ""; | |
private String soapBody = ""; | |
private final static Logger LOG = Logger.getLogger(ThirdParty.class); | |
public ParamedThirdPartyClient setSoapAction(final String soapAction) { | |
this.soapAction = soapAction; | |
return this; | |
} | |
public ParamedThirdPartyClient setSoapBody(final String soapBody) { | |
this.soapBody = soapBody; | |
return this; | |
} | |
protected HttpClient createHttpClient() { | |
final Protocol httpsProtocol = new Protocol("https", new BasicSSLSocketFactory(), 443); | |
Protocol.registerProtocol("https", httpsProtocol); | |
final HttpClient httpClient = new HttpClient(); | |
final String proxyhost = "proxy"; | |
final String proxyport = "123"; | |
final int proxyporti = Integer.parseInt(proxyport); | |
final boolean hasLocalDevelopment = true; | |
if (hasLocalDevelopment) { | |
final boolean USE_NTLM_AUTH = false; | |
final UsernamePasswordCredentials cred = new UsernamePasswordCredentials("xxx","yyy"); | |
httpClient.getState().setAuthenticationPreemptive(USE_NTLM_AUTH); | |
httpClient.getState().setProxyCredentials(AuthScope.ANY, cred); | |
} // End of the if - // | |
return httpClient; | |
} // End of method , create HTTP client // | |
protected TrustManager[] getTrustManagersSimple() throws Exception { | |
return new TrustManager[] { new X509TrustManager() { | |
public X509Certificate[] getAcceptedIssuers() { | |
return null; | |
} | |
public void checkClientTrusted(X509Certificate[] certs, String authType) { | |
} | |
public void checkServerTrusted(X509Certificate[] certs, String authType) { | |
} | |
} } ; | |
} | |
protected KeyManager[] getKeyManagers(String keyStoreType, final InputStream keyStoreFile, final String keyStorePassword) throws Exception { | |
final KeyStore keyStore = KeyStore.getInstance(keyStoreType); | |
if (keyStorePassword != null) { | |
keyStore.load(keyStoreFile, keyStorePassword.toCharArray()); | |
} else { | |
keyStore.load(keyStoreFile, null); | |
} | |
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); | |
kmf.init(keyStore, keyStorePassword.toCharArray()); | |
return kmf.getKeyManagers(); | |
} | |
protected TrustManager[] getTrustManagers(String trustStoreType, InputStream trustStoreFile, String trustStorePassword) throws Exception { | |
final KeyStore trustStore = KeyStore.getInstance(trustStoreType); | |
if (trustStorePassword != null) { | |
trustStore.load(trustStoreFile, trustStorePassword.toCharArray()); | |
} else { | |
trustStore.load(trustStoreFile, null); | |
} | |
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | |
tmf.init(trustStore); | |
return tmf.getTrustManagers(); | |
} | |
protected PostMethod newPostMethod(final String webServiceURL) { | |
final PostMethod postMethod = new PostMethod(webServiceURL); | |
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); | |
return postMethod; | |
} | |
public String makeWebServiceRequest(final String webServiceURL) throws Exception { | |
final HttpClient httpClient = this.createHttpClient(); | |
final PostMethod postMethod = this.newPostMethod(webServiceURL); | |
postMethod.setRequestHeader("Accept", "application/soap+xml,multipart/related,text/*"); | |
postMethod.setRequestHeader("Cache-Control", "no-cache"); | |
postMethod.setRequestHeader("Pragma", "no-cache"); | |
postMethod.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); | |
postMethod.setRequestHeader("SOAPAction", this.soapAction); | |
postMethod.setRequestHeader("Date", String.valueOf(new Date())); | |
final String body = this.soapBody; | |
final int contentLength = body.length(); | |
postMethod.setRequestHeader("Content-Length", String.valueOf(contentLength)); | |
postMethod.setRequestEntity(new StringRequestEntity(body, "text/xml", "utf-8")); | |
final int statusCode = httpClient.executeMethod(postMethod); | |
final byte[] responseBody = postMethod.getResponseBody(); | |
final String webServiceRes = new String(responseBody); | |
return webServiceRes; | |
} // End of the method / POST // | |
} // End of the class // |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment