【问题标题】:Sending SOAP request to a specific service向特定服务发送 SOAP 请求
【发布时间】:2016-09-20 14:19:51
【问题描述】:

好的,所以我对 Web 服务完全陌生,对于我正在从事的项目,我正试图围绕整个 SOAP 事物展开思考。我想我对正在发生的事情有一个模糊的了解,但我错过了一些具体的信息,我根本无法通过谷歌搜索找到任何有用的信息。

我已经阅读过其他人提出的类似SOAP request to WebService with java 的问题,但我仍然无法完全弄清楚发生了什么。

具体来说,我正在尝试使用http://ec.europa.eu/taxation_customs/vies/vatRequest.html 此处提供的服务及其 wsdl 文件http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl

我尝试调整上述问题中给出的示例,但我无法弄清楚要在何处添加哪些值,因此它适用于此特定服务。我得到的只是“405 Method not allowed”响应。这是我尝试的改编:

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

如果有人可以向我解释我到底做错了什么以及如何解决它,或者甚至给我一个可行的例子,我将永远感激...

【问题讨论】:

    标签: java web-services soap wsdl


    【解决方案1】:

    代码必须稍作修改才能使用服务。

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

    是你必须到达的端点(这是来自 wsdl)

    <wsdlsoap:address location="http://ec.europa.eu/taxation_customs/vies/services/checkVatService"/>
    

    请注意,当我点击这个时,我得到了一个肥皂故障。看起来必须再次检查构造的 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>
    

    【讨论】:

    • 非常感谢,现在可以使用了!你认为你可以对 ELI5 这么好,你怎么知道在哪里使用哪些值?或者你知道解释这些东西的教程吗?我永远不会自己找到这个......
    • 很高兴它有帮助。我刚刚提到了docs.oracle.com/cd/E19879-01/819-3669/bnbhw/index.html。我基本上复制粘贴了你的代码。通过在 SOAP UI 工具上生成示例 SOAP 请求,我发现名称空间不正确。我认为需要添加命名空间,我只是这样做了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多