Skip to content

Instantly share code, notes, and snippets.

@andrewnguyen42

andrewnguyen42/Java post

Last active Dec 16, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.