Skip to content

Instantly share code, notes, and snippets.

@andrewnguyen42
Last active December 16, 2015 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewnguyen42/5384029 to your computer and use it in GitHub Desktop.
Save andrewnguyen42/5384029 to your computer and use it in GitHub Desktop.
Send an xml string to a url Requires 5 jars: commons-httpclient-3.0.1.jar , httpclient-4.0-alpha2.jar , httpcore-4.0-alpha6.jar , commons-codec-1.7.jar , commons-logging-1.1.2.jar
/**
* Actually takes care of posting data to a service
* @param xml The xml data in the form of a Java String
* @param url The address to post data to
*/
private static boolean postData(String xml, String url){
//PostMethod post=new PostMethod();
HttpPost pst=null;
try {
//build headers and body of post request
pst=new HttpPost(url);
StringEntity requestEntity = new StringEntity(xml);
pst.setEntity(requestEntity);
pst.setHeader("Content-type",
"text/xml; charset=ISO-8859-1");
DefaultHttpClient httpClient = new DefaultHttpClient();
//TODO use this response to give our client a message
HttpResponse response=httpClient.execute(pst);
int statusCode=response.getStatusLine().getStatusCode();
return statusCode>=400&&statusCode<=499;
}catch (org.apache.http.HttpException| URISyntaxException|IOException | InterruptedException e) {
e.printStackTrace();
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment