2015-08-18 67 views
0

我正在使用maven作爲我的構建tool.trying從我的java代碼調用基於soap的webservice。但是我的查詢出錯了。這個查詢字符串我從soapUI.I用soapUI.it測試了我的web服務,但它現在工作的時候並不工作,而使用Apache http client.someone plz幫助我!使用apache http客戶端調用soap webservice的問題

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) 
+0

URL中的「?wsdl」通常引用WSDL文件的位置,而不是您應該發佈SOAP消息的端點。 –

+0

您提供的使用方法會面臨的另一個問題是,您需要在發送POST之前設置正確的HTTP標頭。現在你不這樣做。 –

回答

0

做正確的做法是使用諸如JAX WS適當的庫。這裏很快說是需要完成得走什麼:

  1. 使用Web服務的WSDL創建能夠 封裝參數傳遞的類和返回的值轉換成可以無縫內使用 的POJO應用程序。
  2. 現在使用一個好的SOAP庫(如JAX WS)使用生成的Java類調用 Web服務。
  3. 處理Web服務的結果。

請參閱this鏈接瞭解如何執行這些活動的更多指導。

+0

Thanks.Even我看到了這個鏈接,但我嘗試了一些不同的方式。可以告訴我我的代碼中出了什麼問題,如果可以的話,請糾正它。 – Dkkarnavar

+0

@Dkkarnavar在soap:Envelope和xmlns之間應該有一個空格。你的POST URL不應該有?wsdl的後綴 - 你應該實際服務端點本身而不是給你WSDL的端點。最後請參閱下面我粘貼的鏈接;似乎已經有一些工作示例:http://stackoverflow.com/questions/10671494/sending-http-post-request-with-soap-action-using-org-apache-http –