Java中调用SOAP Web Service

Postman调试SOAP Web Service

1.使用action路径作为URL

2.请求方法设为POST

3.Content-Type为text/xml

4.body填写SOAP Envelope,header,body

5.示例

postman调试SOAP

Java调用SOAP Web Service

本文档介绍了Java调用SOAP Web Service的简单应用。

一、SOAP

SOAP(Simple Object Access Protocol)是一种交换数据的协议规范,特点是轻量级、基于XML。

请求和响应都是xml的形式,示例如下:

  • request
POST /regcodeService.asmx HTTP/1.1
Host: weixin.fscut.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "https://weixin.fscut.com/MakeRegCode"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <SessionHeader xmlns="https://weixin.fscut.com">
      <LangID>int</LangID>
      <SvcVersion>int</SvcVersion>
      <OEMCode>int</OEMCode>
      <SessionID>string</SessionID>
      <OperFrom>string</OperFrom>
    </SessionHeader>
  </soap:Header>
  <soap:Body>
    <MakeRegCode xmlns="https://weixin.fscut.com">
      <Serial>string</Serial>
      <ExpireDate>string</ExpireDate>
      <Flags>int</Flags>
      <CustomName>string</CustomName>
      <Mobile>string</Mobile>
      <Comment>string</Comment>
    </MakeRegCode>
  </soap:Body>
</soap:Envelope>
  • response
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <MakeRegCodeResponse xmlns="https://weixin.fscut.com">
      <MakeRegCodeResult>
        <Status>int</Status>
        <ErrMsg>string</ErrMsg>
        <Data>string</Data>
      </MakeRegCodeResult>
    </MakeRegCodeResponse>
  </soap:Body>
</soap:Envelope>

由示例可以看出,一条SOAP消息就是一个XML文档,一般包含以下元素:

  • Envelope,标识此XML是一条SOAP消息
  • Header,头部信息
  • Body,调用和响应信息
  • Fault,处理消息时发生的错误信息

按照处理XML的方式读写即可。

二、在Java中应用

参考:

How to do a SOAP Web Service call from Java class

首先要创建客户端发起请求,与URL请求类似,步骤如下:

  • 创建连接
  • 设置MimeHeaders
  • 设置参数(soapHeader + soapBody)
  • 解析response
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:
             https://www.w3schools.com/xml/tempconvert.asmx?op=CelsiusToFahrenheit


            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 = "https://www.w3schools.com/xml/tempconvert.asmx";
        String soapAction = "https://www.w3schools.com/xml/CelsiusToFahrenheit";

        callSoapWebService(soapEndpointUrl, soapAction);
    }

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

        String myNamespace = "myNamespace";
        String myNamespaceURI = "https://www.w3schools.com/xml/";

        // 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="https://www.w3schools.com/xml/">
                <SOAP-ENV:Header/>
                <SOAP-ENV:Body>
                    <myNamespace:CelsiusToFahrenheit>
                        <myNamespace:Celsius>100</myNamespace:Celsius>
                    </myNamespace:CelsiusToFahrenheit>
                </SOAP-ENV:Body>
            </SOAP-ENV:Envelope>
            */

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("CelsiusToFahrenheit", myNamespace);
        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("Celsius", myNamespace);
        soapBodyElem1.addTextNode("100");
    }

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

}

除此之外,设置session header的实例如下

SOAPHeader header = soapMessage.getSOAPHeader();
SOAPHeaderElement headerElement = header.addHeaderElement(new QName(SOAP_NAMESPACE_URI,
        SOAP_KEY_SESSION_HEADER));
SOAPElement langElement = headerElement.addChildElement(SOAP_KEY_LANG_ID);
langElement.addTextNode(SOAP_VALUE_LANG_ID);

response的结果和request类似,可通过解析soapBody的方式获得结果。

处理方式与处理XML类似,不再赘述。