java使用soap方式簡單實現webservice (二)__web

來源:互聯網
上載者:User

上一篇從 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);  }}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.