0
<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?

Noman Arain
  • 1,172
  • 4
  • 19
  • 45
  • 1
    You seem to be trying to write your own SOAP client API. SOAP has HTTP header requirements in addition to the body payload. See [here](http://illegalargumentexception.blogspot.co.uk/2011/04/java-jax-ws-web-services-and-clients.html#ws_doc) for some documentation links. But consider utilizing a Java SOAP library (e.g. JAX-RPC; JAX-WS.) – McDowell Dec 07 '12 at 22:32
  • If you could show me some code please, that would be great. – Noman Arain Dec 08 '12 at 04:43
  • @Nomain Arain - [question 10671494](http://stackoverflow.com/questions/10671494/sending-http-post-request-with-soap-action-using-org-apache-http) may help you. But I recommend using the [JAX-WS](http://illegalargumentexception.blogspot.co.uk/2011/04/java-jax-ws-web-services-and-clients.html) client API for error handling if nothing else. If you want to correctly handle SOAP messages with manual HTTP code you have no choice but to read the specification and implement it. – McDowell Dec 08 '12 at 20:44

1 Answers1

0
/*
     * 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.

Noman Arain
  • 1,172
  • 4
  • 19
  • 45