当前位置: 首页 > 知识库问答 >
问题:

向特定服务发送 SOAP 请求

魏泰
2023-03-14

好的,我对网络服务完全陌生,对于我正在做的一个项目,我试图了解整个SOAP。我想我对正在发生的事情有一个模糊的理解,但是我缺少一些具体的信息,我在谷歌上找不到任何有用的东西。

我已经阅读了其他人提出的问题,例如使用java向Web服务发出的SOAP请求,但我仍然无法完全弄清楚发生了什么。

具体来说,我尝试使用这里提供的服务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);
    }
}

如果有人能向我解释我到底哪里做错了,如何改正,或者甚至给我一个可行的例子,我将永远感激不尽...

共有1个答案

谭成业
2023-03-14

代码必须稍加修改才能命中服务。

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

是您必须点击的endpoint(这来自wsdl)

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

请注意,当我击中这个,我得到一个肥皂错误。看起来必须再次检查构造的 SOAP 正文。

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>
 类似资料:
  • XMLHttpRequest 对象用于和服务器交换数据。 向服务器发送请求 如需将请求发送到服务器,我们使用 XMLHttpRequest 对象的 open() 和 send() 方法:xmlhttp.open("GET","ajax_info.txt",true); xmlhttp.send(); 方法 描述 open(method,url,async) 规定请求的类型、URL 以及是否异步处理

  • 有史以来第一次使用PS,有史以来第一次一般的脚本! 我在主机上本地存储了一个 wsdl,它指向本地主机终结点。这是必要的,因为远程服务器不为 wsdl 提供服务。但是,它们是这些 SOAP 请求的有效终结点。 我的网络中有多个endpoint,我想在引用静态wsdl的同时向每个endpoint发送New-WebServiceProxy请求。是否有办法使用本地wsdl,但指定目标endpoint覆盖

  • 我正在进行一个开发,向远程web服务发送请求,并使用获得响应。 在本例中,我使用下面提到的WSDl的成功地生成了客户端wsdl2java代码。 示例WSDL URL: 在做了一些研究之后,我创建了下面的示例代码,用于向其中定义的web服务发送SOAP请求,并使用生成的客户机代码获得apache Camel的响应。 我只想向远程web服务发送一个SOAP请求,并使用apache Camel获得响应。

  • 我有以下WCF服务操作: 我可以通过导航到服务在浏览器中加载WSDL。但是,当我在Jmeter中向同一服务发送“SOAP/XML-RPC请求”时,我收到了响应代码-400-错误请求。 以下是我正在使用的Soap消息:

  • 问题内容: 是否可以将JAX-RS Web服务重定向到另一个网页? 就像您使用Servlet一样。 JAX-RS Web服务本身应该重定向。如果相关,我正在使用RESTEasy。 问题答案: 是的,如果您的返回类型是(或) https://eclipse- ee4j.github.io/jersey.github.io/apidocs/1.19.1 ,则可以在Jersey或任何JAX- RS实现(