【问题标题】:Issue on calling soap webservice using apache http client使用apache http客户端调用soap webservice的问题
【发布时间】:2015-11-11 15:20:57
【问题描述】:

我使用 maven 作为我的构建工具。试图从我的 java 代码中调用基于 soap 的 web 服务。但是我的查询出了点问题。这个查询字符串我取自 soapUI。我用 soapUI 测试了我的 web 服务。它正在工作.但现在它在使用 apache http 客户端时无法正常工作。有人请帮助我!!

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)

【问题讨论】:

  • ?wsdl 在 URL 中通常引用 WSDL 文件的位置,而不是您应该发布 SOAP 消息的端点。
  • 使用您提供的方法您将面临的另一个问题是,您需要在发送 POST 之前设置正确的 HTTP 标头。现在你不这样做了。

标签: java web-services soap soapui


【解决方案1】:

正确的做法是使用像 JAX WS 这样的适当库。在这里快速说明的是需要做的事情:

  1. 使用 Web 服务的 WSDL 创建类 将要传递的参数和返回的值封装成 可以在应用程序中无缝使用的 POJO。
  2. 现在使用一个好的 SOAP 库比如 JAX WS 来调用 使用生成的 Java 类的 Web 服务。
  3. 处理 Web 服务的结果。

有关如何执行这些活动的更多指导,请参阅this 链接。

【讨论】:

  • 谢谢。即使我以前看过这个链接。但我尝试了不同的方法。你能告诉我我的代码有什么问题吗?如果可能的话更正它。
  • @Dkkarnavarsoap:Envelope 和 xmlns 之间应该有一个空格。此外,您的 POST URL 不应该有 ?wsdl 后缀 - 您应该点击实际的服务端点本身,而不是给您提供的那个WSDL。最后参考我下面贴的链接;似乎已经有一些工作示例:stackoverflow.com/questions/10671494/…
猜你喜欢
  • 2020-07-07
  • 2010-09-16
  • 2014-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 2018-03-02
相关资源
最近更新 更多