Noman Arain Noman Arain - 1 year ago 63
Java Question

How to post xml over HTTPS in java?

<env:Envelope xmlns:env=""^M
</wsse:Security> ^M

I have been told to post this xml to:
I am also given the following method:

public int sendPostRequest(String url, String content, String contentType)
throws Exception {
PostMethod post = new PostMethod(url);
post.setRequestEntity(new StringRequestEntity(content, contentType,
boolean success = false;
String responseBody = null;
int statusCode;

try {
success = true;
statusCode = post.getStatusCode();
responseBody = post.getResponseBodyAsString();
} catch (Exception ex) {
log.error("Marhsalling exception : " + ex.getMessage());
throw new InvalidRequestException("Marhsalling exception :"
+ ex.getMessage());
} finally {

if ((statusCode != HttpStatus.SC_OK) &&
(statusCode != HttpStatus.SC_NO_CONTENT)) {
String error = "Got Bad Http Status - <" + statusCode
+ "> Info : " + responseBody;
throw new InvalidRequestException(error);
} else {
log.debug("Success - " + responseBody);
return statusCode;

private String footer = "</env:Envelope>";
private String message = "<InstallService><NewAccount></NewAccount></InstallService>";
private String payload = header + message + footer;

header I have been told is the XML that I have posted. I am not sure about the content type, it was suggested that it could be XML. The project type should be the dynamic web project using the Tomcat server. I was also told to grab org.apache.commons.httpclient library.

I have been trying to put the pieces together but I'm failing to. I get error at: getHttpClient(), saying that the method could not be resolved. It is same for log and InvalidRequestException could not be resolved. It seems that I have to extend my class to another class which would contain these three methods. Which class could it be? What jars could I be missing? A simple main method which calls the above method and passes the required arguments would work?

Answer Source
     * soapXMLtoEndpoint sends the soapXMLFileLocation to the endpointURL
    public void soapXMLtoEndpoint(String endpointURL, String soapXMLFileLocation) throws SOAPException {
        SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
        SOAPMessage response =, endpointURL);
        SOAPBody responseBody = response.getSOAPBody();
        SOAPBodyElement responseElement = (SOAPBodyElement) responseBody.getChildElements().next();
        SOAPElement returnElement = (SOAPElement) responseElement.getChildElements().next();
        if (responseBody.getFault() != null) {
            System.out.println("fault != null");
            System.out.println(returnElement.getValue() + " " + responseBody.getFault().getFaultString());
        } else {
            serverResponse = returnElement.getValue();
            System.out.println("\nfault == null, got the response properly.\n");

Using file in this case, but it can be a simple string. You would have to create a soapmessage out of that string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download