使用java向WebService发送SOAP请求

Pie*_*vis 36 java soap web-services

我对如何通过java向web服务发出请求感到困惑.

目前,我唯一理解的是webservices使用xml结构化消息,但我仍然不太了解如何构造我的请求.

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getProductDetails xmlns="http://magazzino.example.com/ws">
      <productId>827635</productId>
    </getProductDetails>
  </soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)

基本上我要向Web服务发送2个参数,作为回报,我期待另外两个参数.

我想有些罐子可以完成大部分工作,但我没有在网上找到任何东西.有人可以解释一下基础吗?

acd*_*ior 83

SOAP请求是一个XML文件,由您发送到服务器的参数组成.

SOAP响应同样是一个XML文件,但现在包含服务想要提供的所有内容.

基本上,WSDL是一个XML文件,它解释了这两个XML的结构.


要在Java中实现简单的SOAP客户端,您可以使用SAAJ框架(它随JSE 1.6及更高版本一起提供):

SOAP with Attachments API for Java(SAAJ)主要用于直接处理任何Web Service API中幕后发生的SOAP请求/响应消息.它允许开发人员直接发送和接收soap消息,而不是使用JAX-WS.

请参阅下面的使用SAAJ的SOAP Web服务调用的工作示例(运行它!).它称之为Web服务.

import javax.xml.soap.*;

public class SOAPClientSAAJ {

    // SAAJ - SOAP Client Testing
    public static void main(String args[]) {
        /*
            The example below requests from the Web Service at:
             http://www.webservicex.net/uszip.asmx?op=GetInfoByCity


            To call other WS, change the parameters below, which are:
             - the SOAP Endpoint URL (that is, where the service is responding from)
             - the SOAP Action

            Also change the contents of the method createSoapEnvelope() in this class. It constructs
             the inner part of the SOAP envelope that is actually sent.
         */
        String soapEndpointUrl = "http://www.webservicex.net/uszip.asmx";
        String soapAction = "http://www.webserviceX.NET/GetInfoByCity";

        callSoapWebService(soapEndpointUrl, soapAction);
    }

    private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String myNamespace = "myNamespace";
        String myNamespaceURI = "http://www.webserviceX.NET";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);

            /*
            Constructed SOAP Request Message:
            <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:myNamespace="http://www.webserviceX.NET">
                <SOAP-ENV:Header/>
                <SOAP-ENV:Body>
                    <myNamespace:GetInfoByCity>
                        <myNamespace:USCity>New York</myNamespace:USCity>
                    </myNamespace:GetInfoByCity>
                </SOAP-ENV:Body>
            </SOAP-ENV:Envelope>
            */

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("GetInfoByCity", myNamespace);
        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("USCity", myNamespace);
        soapBodyElem1.addTextNode("New York");
    }

    private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
        try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);

            // Print the SOAP Response
            System.out.println("Response SOAP Message:");
            soapResponse.writeTo(System.out);
            System.out.println();

            soapConnection.close();
        } catch (Exception e) {
            System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
            e.printStackTrace();
        }
    }

    private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        createSoapEnvelope(soapMessage);

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", soapAction);

        soapMessage.saveChanges();

        /* Print the request message, just for debugging purposes */
        System.out.println("Request SOAP Message:");
        soapMessage.writeTo(System.out);
        System.out.println("\n");

        return soapMessage;
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 完美的例子.一个问题,读取xml响应输出的首选方法是什么?谢谢. (2认同)
  • 我如何传递基本的身份验证详细信息? (2认同)

lka*_*mal 7

当WSDL可用时,您只需要遵循两个步骤来调用该Web服务.

第1步:从WSDL2Java工具生成客户端源

第2步:使用以下命令调用操作:

YourService service = new YourServiceLocator();
Stub stub = service.getYourStub();
stub.operation();
Run Code Online (Sandbox Code Playgroud)

如果再往前看,您会注意到Stub该类用于调用远程位置部署的服务作为Web服务.在调用它时,您的客户端实际上会生成SOAP请求并进行通信.类似地,Web服务将响应作为SOAP发送.在Wireshark之​​类的工具的帮助下,您可以查看交换的SOAP消息.

但是,由于您已经要求对基础知识进行更多说明,我建议您在此处参考并与其客户编写Web服务以进一步学习.