Java调用WebService接口--SOAPMessage组装参数

使用SOAPMessage进行WebService接口调用需要自己构造请求参数,需要按照一定的顺序,按照一定的层级关系添加组装成xml格式报文。一下是示例代码。

使用demo代码

package cn.hewie.webService.client;

import com.sun.xml.internal.ws.client.BindingProviderProperties;
import com.sun.xml.internal.ws.developer.JAXWSProperties;
import org.w3c.dom.Document;

import javax.xml.namespace.QName;
import javax.xml.soap.*;
import javax.xml.ws.Dispatch;
import java.net.URL;
import java.util.HashMap;

public class SoapRequest {
    public void execute() throws Exception {
        String nameSpace = "http://tempuri.org/";
        String wsdlUrl = "https://infotrack.naqelexpress.com/NaqelAPIServices/NaqelAPIDemo/5.0/XMLShippingService.asmx?WSDL";
        String serviceName = "XMLShippingService";
        String portName = "XMLShippingServiceSoap12";
        String elementName = "IsWaybillExists";
        String responseName = "IsWaybillExistsResponse";

        // 1.create service
        URL url = new URL(wsdlUrl);
        QName sname = new QName(nameSpace, serviceName);
        javax.xml.ws.Service service = javax.xml.ws.Service.create(url, sname);

        // 2.create Dispatch object
        Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(nameSpace, portName), SOAPMessage.class, javax.xml.ws.Service.Mode.MESSAGE);

        // 3.create SOAPMessage
        SOAPMessage msg = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
        msg.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");

        // 3.1 create SOAPEnvelope
        SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
        envelope.setPrefix("soap");
        envelope.addNamespaceDeclaration("tem", nameSpace);
        envelope.removeAttribute("xmlns:env");

        // 3.2 create SOAPHeader
        SOAPHeader header = envelope.getHeader();
        if (null == header) {
            header = envelope.addHeader();
        }
        header.setPrefix("soap");

        // 3.3 create SOAPBody
        SOAPBody body = envelope.getBody();
        body.setPrefix("soap");
        SOAPBodyElement clientInfo = body.addBodyElement(new QName(nameSpace, "ClientInfo", "tem"));
        clientInfo.addChildElement(new QName(nameSpace, "ClientID", "tem")).setValue("1");
        clientInfo.addChildElement(new QName(nameSpace, "Password", "tem")).setValue("111");
        clientInfo.addChildElement(new QName(nameSpace, "Version", "tem")).setValue("1.0");
        SOAPBodyElement ele = body.addBodyElement(new QName(nameSpace, elementName, "tem"));
        ele.addChildElement(clientInfo);
        ele.addChildElement(new QName(nameSpace, "WaybillNo", "tem")).setValue("111");
        // ...

        msg.writeTo(System.out); // print xml
        System.out.println("\n----------");

        // set timeout
        dispatch.getRequestContext().put(BindingProviderProperties.CONNECT_TIMEOUT, 2000);
        dispatch.getRequestContext().put(JAXWSProperties.REQUEST_TIMEOUT, 2000);
        SOAPMessage response = dispatch.invoke(msg);

        // 4. get response and transfer to dom object
        Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
        String result = doc.getElementsByTagName(responseName).item(0).getTextContent();
        System.out.println(result);
    }

}

上例中一些变量值的获取肯定让人有点摸不着头脑,我们可以通过wsdl文档获取到其中的参数变量的值。

参数获取途径

以下是使用soapUI打开的wsdl文档,并发送的一条请求,示例代码中的变量标识如下:
Java调用WebService接口--SOAPMessage组装参数_第1张图片

样例结果

调用上述execute方法,样例结果如下:


  
  
    
      
        1
        111
        1.0
      
      111
    
  

----------
true

你可能感兴趣的:(Java)