上一篇從 xml 中找對應的參數比較麻煩,這裡介紹用 SOAPUI 的方式填寫參數
這裡為實際調用 .net的 簡訊發送服務測試
import java.net.URL;import javax.xml.namespace.QName;import javax.xml.soap.MessageFactory;import javax.xml.soap.SOAPBody;import javax.xml.soap.SOAPBodyElement;import javax.xml.soap.SOAPConstants;import javax.xml.soap.SOAPEnvelope;import javax.xml.soap.SOAPMessage;import javax.xml.ws.Dispatch;import javax.xml.ws.Service;import org.w3c.dom.Document;public class Test {public static void main(String[] args) throws Exception {// {*} * 為圖片中的數字 String ns = "http://tempuri.org/"; // {1} String wsdlUrl = "http://work.dahuatech.com/Application/Service/MsgSmsService.asmx?WSDL"; // {2} //1、建立服務(Service) URL url = new URL(wsdlUrl); QName sname = new QName(ns, "MsgSmsService"); // {3}Service service = Service.create(url, sname); //2、建立Dispatch Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns, "MsgSmsServiceSoap12"), SOAPMessage.class, Service.Mode.MESSAGE); // {4} //3、建立SOAPMessage SOAPMessage msg = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(); SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope(); SOAPBody body = envelope.getBody(); //4、建立QName來指定訊息中傳遞資料 QName ename = new QName(ns, "SendPlmMsg", "tem");//<nn:add xmlns="xx"/> // {5} SOAPBodyElement ele = body.addBodyElement(ename); // 傳遞參數 // {6} ele.addChildElement("strMobiles", "tem").setValue("151****3701"); ele.addChildElement("strMSg", "tem").setValue("測試!"); msg.writeTo(System.out); System.out.println("\n invoking....."); //5、通過Dispatch傳遞訊息,會返迴響應訊息 SOAPMessage response = dispatch.invoke(msg); response.writeTo(System.out); System.out.println(); //6、響應訊息處理,將響應的訊息轉換為dom對象 Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument(); String str = doc.getElementsByTagName("SendPlmMsgResult").item(0).getTextContent(); // {7} System.out.println(str); }}