使用apache http客户端调用soap webservice的问题

时间:2015-08-18 15:30:36

标签: java web-services soap soapui

我正在使用maven作为我的构建工具。从我的java代码调用基于soap的webservice.But我的查询出了问题。这个查询字符串我从soapUI.I测试我的webservice用soapUI.it工作了。但现在它在使用apache http client.someone plz帮助我时无法正常工作!!

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();
                }
        }
    }
}

=======编译出来========

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)

1 个答案:

答案 0 :(得分:0)

正确的做法是使用像JAX WS这样的合适的库。这里快速说明了要开展的工作:

  1. 使用Web服务的WSDL创建可以使用的类 封装要传递的参数和返回的值 可以在应用程序中无缝使用的POJO。
  2. 现在使用一个好的SOAP库(如JAX WS)来调用 使用生成的Java类的Web服务。
  3. 处理网络服务的结果。
  4. 有关如何执行这些活动的更多指导,请参阅this链接。

相关问题