java - 向特定服务发送 SOAP 请求

标签 java web-services soap wsdl

好吧,我对 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 方法不允许”响应。这是我尝试的改编:

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

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

最佳答案

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

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

是您必须访问的端点(来自 wsdl)

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

请注意,当我点击此按钮时,我遇到了 SOAP 错误。看起来必须再次检查构造的 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>

关于java - 向特定服务发送 SOAP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39596606/

相关文章:

java - maven构建错误

java - 从另一个正在运行的 Java 应用程序捕获异常

java - Spring 将文件映射到 Url/URI

android - KSOAP2 - 连接被拒绝

php - Soapclient 不解析 WSDL(使用 SSL 证书和 wsse)

java - 出现空格后如何区分同一字符串中的不同字符

java - 使用 java 验证 smtp 服务器凭据而不实际发送邮件

android - 有没有更好的方法在 Android 中解析 SoapObject

java - 内部服务器错误 axis2 Tomcat 服务 wsdl

java - 生成 WebService 工件时遇到问题