Tuesday, January 17, 2012

How to generate HTTP post request to server/service using java

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;


  serviceEndpoint = "end point of the service";

        webServicePayload = "xml file in the form of string";

        HttpClient client = new HttpClient();
        // TODO make a service end point change?
        PostMethod post = new PostMethod(serviceEndpoint);

        StringRequestEntity body = new StringRequestEntity(webServicePayload,
                "text/xml", "UTF-8");
        post.addRequestHeader("SOAPAction", "\"\"");
        post.setRequestEntity(body);

      
        int status = client.executeMethod(post);

        String responseBody = post.getResponseBodyAsString();

No comments:

Post a Comment