2016-09-20 94 views
0

好吧,所以我完全不熟悉web服務和我正在開發的項目,我正在試圖圍繞整個SOAP事物進行包裝。我認爲我對發生的事情有一個模糊的理解,但我錯過了一些具體的信息,而且我根本找不到任何有助於Google搜索的東西。發送SOAP請求到特定的服務

我讀過其他人問這樣的問題SOAP request to WebService with java,但我仍然無法完全弄清楚發生了什麼。

具體我試圖使用與這裏的WSDL文件http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl

我試圖適應在對上述問題給出的例子在這裏http://ec.europa.eu/taxation_customs/vies/vatRequest.html提供的服務,但我想不出什麼值增加地方所以它適用於這個特定的服務。我所得到的是一個「405方法不允許」的迴應。這是我試圖適應:

package at.kmds.soaptest; 

import javax.xml.soap.*; 
import javax.xml.transform.Source; 
import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.stream.StreamResult; 

public class Main { 
    public static void main(String args[]) { 
     try { 
      // Create SOAP Connection 
      SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
      SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 

      // Send SOAP Message to SOAP Server 
      String url = "http://ec.europa.eu/taxation_customs/vies/vatRequest.html"; 
      SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url); 

      // Process the SOAP Response 
      printSOAPResponse(soapResponse); 

      soapConnection.close(); 
     } catch (Exception e) { 
      System.err.println("Error occurred while sending SOAP Request to Server"); 
      e.printStackTrace(); 
     } 
    } 

    private static SOAPMessage createSOAPRequest() throws Exception { 
     MessageFactory messageFactory = MessageFactory.newInstance(); 
     SOAPMessage soapMessage = messageFactory.createMessage(); 
     SOAPPart soapPart = soapMessage.getSOAPPart(); 

     String serverURI = "http://ec.europa.eu/"; 

     // SOAP Envelope 
     SOAPEnvelope envelope = soapPart.getEnvelope(); 
     envelope.addNamespaceDeclaration("example", serverURI); 

     // SOAP Body 
     SOAPBody soapBody = envelope.getBody(); 
     SOAPElement soapBodyElem = soapBody.addChildElement("checkVat"); 
     SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("countryCode"); 
     soapBodyElem1.addTextNode("..."); 
     SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("vatNumber"); 
     soapBodyElem2.addTextNode("..."); 

     MimeHeaders headers = soapMessage.getMimeHeaders(); 
     headers.addHeader("SOAPAction", serverURI + "checkVat"); 

     soapMessage.saveChanges(); 

     /* Print the request message */ 
     System.out.print("Request SOAP Message = "); 
     soapMessage.writeTo(System.out); 
     System.out.println(); 

     return soapMessage; 
    } 

    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception { 
     TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
     Transformer transformer = transformerFactory.newTransformer(); 
     Source sourceContent = soapResponse.getSOAPPart().getContent(); 
     System.out.print("\nResponse SOAP Message = "); 
     StreamResult result = new StreamResult(System.out); 
     transformer.transform(sourceContent, result); 
    } 
} 

如果有人能向我解釋,究竟我做錯了,如何解決這個問題,或者甚至給我一個工作的例子,我會感激不盡......

回答

1

代碼必須稍作修改才能使用該服務。

String url = "http://ec.europa.eu/taxation_customs/vies/services/checkVatService"; 

是你打的端點(這是從WSDL)

​​

需要注意的是,當我打,我得到一個肥皂fault.Looks等構成的SOAPBody將不得不再次檢查。

Request SOAP Message = <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ec.europa.eu/"><SOAP-ENV:Header/><SOAP-ENV:Body><checkVat><countryCode>...</countryCode><vatNumber>...</vatNumber></checkVat></SOAP-ENV:Body></SOAP-ENV:Envelope> 

Response SOAP Message = <?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>Unexpected wrapper element checkVat found. Expected {urn:ec.europa.eu:taxud:vies:services:checkVat:types}checkVat.</faultstring></soap:Fault></soap:Body></soap:Envelope> 

編輯一個完整的程序,工作(看起來像),給我無效的輸入,因爲我通過點(...)。

import javax.xml.namespace.QName; 
import javax.xml.soap.*; 
import javax.xml.transform.Source; 
import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.stream.StreamResult; 

public class Main { 
    public static void main(String args[]) { 
     try { 
      // Create SOAP Connection 
      SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
      SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 

      // Send SOAP Message to SOAP Server 
      String url = "http://ec.europa.eu/taxation_customs/vies/services/checkVatService"; 
      SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url); 

      // Process the SOAP Response 
      printSOAPResponse(soapResponse); 

      soapConnection.close(); 
     } catch (Exception e) { 
      System.err.println("Error occurred while sending SOAP Request to Server"); 
      e.printStackTrace(); 
     } 
    } 

    private static SOAPMessage createSOAPRequest() throws Exception { 
     MessageFactory messageFactory = MessageFactory.newInstance(); 
     SOAPMessage soapMessage = messageFactory.createMessage(); 
     SOAPPart soapPart = soapMessage.getSOAPPart(); 

     String serverURI = "http://ec.europa.eu/"; 

     // SOAP Envelope 
     SOAPEnvelope envelope = soapPart.getEnvelope(); 
     envelope.addNamespaceDeclaration("tns1", "urn:ec.europa.eu:taxud:vies:services:checkVat:types"); 
     envelope.addNamespaceDeclaration("impl", "urn:ec.europa.eu:taxud:vies:services:checkVat"); 

     // SOAP Body 
     SOAPBody soapBody = envelope.getBody(); 
     QName bodyQName = new QName("urn:ec.europa.eu:taxud:vies:services:checkVat:types", 
       "checkVat", "tns1"); 
     SOAPElement soapBodyElem = soapBody.addChildElement(bodyQName); 

     SOAPElement soapBodyElem1 = soapBodyElem.addChildElement(new QName("urn:ec.europa.eu:taxud:vies:services:checkVat:types", 
       "countryCode", "tns1")); 
     soapBodyElem1.addTextNode("..."); 
     SOAPElement soapBodyElem2 = soapBodyElem.addChildElement(new QName("urn:ec.europa.eu:taxud:vies:services:checkVat:types", 
       "vatNumber", "tns1")); 
     soapBodyElem2.addTextNode("..."); 

     MimeHeaders headers = soapMessage.getMimeHeaders(); 
     headers.addHeader("SOAPAction", serverURI + "checkVat"); 

     soapMessage.saveChanges(); 

     /* Print the request message */ 
     System.out.print("Request SOAP Message = "); 
     soapMessage.writeTo(System.out); 
     System.out.println(); 

     return soapMessage; 
    } 

    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception { 
     TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
     Transformer transformer = transformerFactory.newTransformer(); 
     Source sourceContent = soapResponse.getSOAPPart().getContent(); 
     System.out.print("\nResponse SOAP Message = "); 
     StreamResult result = new StreamResult(System.out); 
     transformer.transform(sourceContent, result); 
    } 
} 

還給

<?xml version="1.0" encoding="UTF-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
     <soap:Fault> 
     <faultcode>soap:Server</faultcode> 
     <faultstring>INVALID_INPUT</faultstring> 
     </soap:Fault> 
    </soap:Body> 
</soap:Envelope> 
+0

太感謝你了,現在的工作!你認爲你可以對ELI5如此善良,你怎麼知道哪些值在哪裏使用?或者你知道一個解釋這個東西的教程嗎?我從來不會去找到這個...... – Szernex

+1

很高興幫助。我剛剛提到了https://docs.oracle.com/cd/E19879-01/819-3669/bnbhw/index.html。我基本上覆制粘貼你的代碼。通過在SOAP UI工具上生成示例SOAP請求,我發現名稱空間不正確。我認爲需要添加命名空間,我只是這樣做了。 –

+0

好的,我會再看一下,再次感謝! – Szernex