SOAP消息的传递
上一篇說了SOAP消息的創建,那么創建好了的SOAP消息要怎么發送給服務端呢?
public class SoapTest {private String wsdlUri = "http://localhost:9999/ns?wsdl";private String ns = "http://lenve.server/";@Testpublic void test3() {try {// 1.創建服務ServiceURL url = new URL(wsdlUri);QName sname = new QName(ns, "MyServerImplService");Service service = Service.create(url, sname);// 2.創建DispatchDispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns,"MyServerImplPort"), SOAPMessage.class, Service.Mode.MESSAGE);//3.創建SOAPMessageSOAPMessage msg = MessageFactory.newInstance().createMessage();SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();//4.創建QName來指定消息中傳遞的數據QName ename = new QName(ns,"add","ns");SOAPBodyElement ele = body.addBodyElement(ename);ele.addChildElement("a").setValue("3");ele.addChildElement("b").setValue("6");//5.通過Dispatch傳遞消息,同時收到響應消息SOAPMessage response = dispatch.invoke(msg);response.writeTo(System.out);Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();String str = doc.getElementsByTagName("addResult").item(0).getTextContent();System.out.println();System.out.println(str);} catch (SOAPException | IOException e) {e.printStackTrace();}}
} 客戶端輸出:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><S:Body><ns2:addResponse xmlns:ns2="http://lenve.server/"><addResult>9</addResult></ns2:addResponse></S:Body></S:Envelope>
9
成功調用了服務端程序。代碼中先定義了兩個變量,第一個是地址,這個不用多解釋,第二個是命名空間,這是從地址所表示的頁面中得到的。,在創建dispatch是還用到了MyServerImplPort,這個也是從文檔中獲得,在文檔的結尾。
。
轉載于:https://www.cnblogs.com/lenve/p/4517985.html
總結
- 上一篇: 个性签名大全2017
- 下一篇: 一度电是多少钱啊?