0

I'm using maven as my build tool.trying to call soap based webservice from my java code.But something went wrong with my query.This query string i taken from soapUI.I tested my webservice with soapUI.it was working.But now its not working while working with apache http client.someone plz help me!!

public class TestMainClient {

    public static void main(String[] args) {

        HttpClient httpObj = new HttpClient();
        httpObj.getParams().setParameter("http.useragent", "Web   Service    Test Client");
        BufferedReader br = null;

        String data = "<soap:Envelopexmlns:soap=\"http://www.w3.org/2003/05/soap-envelope/\" xmlns:web=\"http://webservice.OrderProcessor/\""
                + " xmlns:xsd=\"http://dto.order.cloudbill.com/xsd/\">"


                + "<soap:Header/>"
                + "<soap:Body>"
                + "<web:placeOrder>"
                + "<web:placeOrderRequest>"
                + "<xsd:orderDate>2015-01-16T09:00:00</xsd:orderDate>"
                + "<xsd:orderDescription>order has two elements</xsd:orderDescription>"
                + "<xsd:orderElementRequests>"
                + "<xsd:orderElementRequest>"
                + "<xsd:createdDate>2015-01-16T09:00:00</xsd:createdDate>"
                + " <xsd:orderElementAttributeRequests>"
                + "<xsd:arrayOrderElementAttribute>"
                + "<xsd:attributeName>Hard disk</xsd:attributeName>"
                + "<xsd:attributeValue>1gb</xsd:attributeValue>"

                + "<xsd:createdDate>2015-01-16T09:00:00</xsd:createdDate>"
                + "</xsd:arrayOrderElementAttribute>"
                + "</xsd:orderElementAttributeRequests>"
                + "<xsd:order_item_name>Laptop</xsd:order_item_name>"
                + " <xsd:order_item_price>20000</xsd:order_item_price>"
                + " </xsd:orderElementRequest>"
                + "</xsd:orderElementRequests>"
                + "</web:placeOrderRequest>"
                + "</web:placeOrder>"
                + "</soap:Body>"
                + "</soap:Envelope>";

        PostMethod methodPost = new PostMethod("http://localhost:8081/OrderManagementService/services/OrderManagementService?wsdl");

        methodPost.setQueryString(data);
        methodPost.setRequestHeader("Content-Type", "text/xml");

        try {
            int returnCode = httpObj.executeMethod(methodPost);**Line 51 **

            if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
                System.out.println("The Post method is not implemented by this URI");
                /*methodPost.getResponseBodyAsString();*/

            } else {
                br = new BufferedReader(new InputStreamReader(methodPost.getResponseBodyAsStream()));
                String readLine;
                while (((readLine = br.readLine()) != null)) {

                    System.out.println(readLine);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            methodPost.releaseConnection();
            if (br != null)
                try {
                    br.close();
                } catch (Exception fe) {
                    fe.printStackTrace();
                }
        }
    }
}

=======compiler out========

org.apache.commons.httpclient.URIException: Invalid query
    at org.apache.commons.httpclient.URI.parseUriReference(URI.java:2049)
    at org.apache.commons.httpclient.URI.<init>(URI.java:147)
    at org.apache.commons.httpclient.HttpMethodBase.getURI(HttpMethodBase.java:265)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:383)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
    at com.module.model.TestMainClient.main(TestMainClient.java:51)
seenukarthi
  • 8,241
  • 10
  • 47
  • 68
  • 1
    `?wsdl` in URL usually references to location of WSDL file, not an endpoint where you should POST your SOAP message. – Alex Salauyou Aug 18 '15 at 15:38
  • Another problem you'll face with using approach you provided, is that you need to set up proper HTTP headers before sending POST. Now you don't do this. – Alex Salauyou Aug 18 '15 at 15:47

1 Answers1

0

The right thing to do is to use a proper library like JAX WS. Quickly stated here is what needs to be done to get going:

  1. Use the web service's WSDL to create the classes that would encapsulate the parameters to be passed and the values returned into POJOs that can be used seamlessly within the application.
  2. Now use a good SOAP library such as JAX WS to invoke the web service using the generated Java classes.
  3. Process the result of the web service.

Refer to this link for more guidance on how to go about performing these activities.

Community
  • 1
  • 1
Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22
  • Thanks.Even I seen this link before.but I tried something different way.Can you tell me what's wrong in my code.if possible correct it. – Dkkarnavar Aug 18 '15 at 16:14
  • @Dkkarnavar There should be a space between soap:Envelope and xmlns.Also your POST URL should not have the ?wsdl suffixed - you should be hitting the actual service endpoint itself not the one that gives you the WSDL. Finally refer to the link I paste below; seems like there are already some working examples :http://stackoverflow.com/questions/10671494/sending-http-post-request-with-soap-action-using-org-apache-http – Prahalad Deshpande Aug 19 '15 at 15:52