Noman Arain Noman Arain - 19 days ago 10
Java Question

How to post xml over HTTPS in java?

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"^M
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"^M
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"^M
xmlns:xsd="http://www.w3.org/2001/XMLSchema"^M
xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">^M
<env:Header>^M
<wsse:Security>^M
<wsse:UsernameToken>^M
<wsse:Username>user</wsse:Username>^M
<wsse:Password>pass</wsse:Password>^M
</wsse:UsernameToken>^M
</wsse:Security> ^M
</env:Header>


I have been told to post this xml to: https://www.abc.com
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,
null));
boolean success = false;
String responseBody = null;
int statusCode;

try {
getHttpClient().executeMethod(post);
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 {
post.releaseConnection();
}

if ((statusCode != HttpStatus.SC_OK) &&
(statusCode != HttpStatus.SC_NO_CONTENT)) {
String error = "Got Bad Http Status - <" + statusCode
+ "> Info : " + responseBody;
log.error(error);
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
/*
     * soapXMLtoEndpoint sends the soapXMLFileLocation to the endpointURL
     */
    public void soapXMLtoEndpoint(String endpointURL, String soapXMLFileLocation) throws SOAPException {
        SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
        SOAPMessage response = connection.call(xmlStringToSOAPMessage(soapXMLFileLocation), endpointURL);
        connection.close();
        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(serverResponse);
            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.

Comments