2012-12-07 17 views
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> 

有人告訴我,這個XML發佈到:https://www.abc.com 我也給出了下面的方法:如何在java中通過HTTPS發佈xml?

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; 

頭有人告訴我的是,我已經發布了XML。我不確定內容類型,有人建議它可以是XML。項目類型應該是使用Tomcat服務器的動態Web項目。我也被告知要抓取org.apache.commons.httpclient庫。

我一直在試圖把碎片放在一起,但我沒有。我得到錯誤:getHttpClient(),說該方法無法解決。它與log相同,InvalidRequestException無法解析。似乎我必須將我的課程擴展到另一個包含這三種方法的課程。它可以是哪一類?我可能錯過哪些罐子?調用上述方法並傳遞所需參數的一個簡單的主要方法可行嗎?

+1

您似乎在嘗試編寫您自己的SOAP客戶端API。除了主體有效載荷以外,SOAP還具有HTTP標頭要求。有關文檔鏈接,請參閱[這裏](http://illegalargumentexception.blogspot.co.uk/2011/04/java-jax-ws-web-services-and-clients.html#ws_doc)。但考慮使用Java SOAP庫(例如JAX-RPC; JAX-WS)。 – McDowell

+0

如果您可以向我展示一些代碼,那就太棒了。 –

+0

@Nomain Arain - [問題10671494](http://stackoverflow.com/questions/10671494/sending-http-post-request-with-soap-action-using-org-apache-http)可能會幫助你。但是,如果沒有,我推薦使用[JAX-WS](http://illegalargumentexception.blogspot.co.uk/2011/04/java-jax-ws-web-services-and-clients.html)客戶端API進行錯誤處理其他。如果你想用手動HTTP代碼正確處理SOAP消息,你別無選擇,只能閱讀規範並實現它。 – McDowell

回答

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

在這種情況下使用文件,但它可以是一個簡單的字符串。你將不得不從該字符串中創建一個soapmessage。