使用JAX-WS调用WebService的短信接口

来源:互联网 发布:淘宝超人软件 编辑:程序博客网 时间:2024/06/06 00:00

1、首先这个接口的WSDL如下:

  <?xml version="1.0" encoding="UTF-8" ?> - <wsdl:definitions targetNamespace="http://短信服务器IP/qxt_jtd/service/SmsService" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://短信服务器IP/qxt_jtd/service/SmsService" xmlns:intf="http://短信服务器IP/qxt_jtd/service/SmsService" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns1="http://io.java" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">- <!-- WSDL created by Apache Axis version: 1.4Built on Apr 22, 2006 (06:55:48 PDT)  --> - <wsdl:types>- <schema targetNamespace="http://短信服务器IP/qxt_jtd/service/SmsService" xmlns="http://www.w3.org/2001/XMLSchema">  <import namespace="http://io.java" />   <import namespace="http://schemas.xmlsoap.org/soap/encoding/" /> - <complexType name="ArrayOf_xsd_string">- <complexContent>- <restriction base="soapenc:Array">  <attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:string[]" />   </restriction>  </complexContent>  </complexType>  </schema>  </wsdl:types>- <wsdl:message name="listSortedFilesResponse">  <wsdl:part name="listSortedFilesReturn" type="impl:ArrayOf_xsd_string" />   </wsdl:message>- <wsdl:message name="getMoResponse">  <wsdl:part name="getMoReturn" type="xsd:string" />   </wsdl:message>- <wsdl:message name="smsFileNameRequest">  <wsdl:part name="tag" type="xsd:string" />   </wsdl:message>- <wsdl:message name="getReportResponse1">  <wsdl:part name="getReportReturn" type="xsd:string" />   </wsdl:message>- <wsdl:message name="getBalanceResponse1">  <wsdl:part name="getBalanceReturn" type="xsd:string" />   </wsdl:message>- <wsdl:message name="sendSmsRequest">  <wsdl:part name="username" type="xsd:string" />   <wsdl:part name="pwd" type="xsd:string" />   <wsdl:part name="phone" type="xsd:string" />   <wsdl:part name="content" type="xsd:string" />   <wsdl:part name="gwid" type="xsd:string" />   </wsdl:message>- <wsdl:message name="sendSmsResponse">  <wsdl:part name="sendSmsReturn" type="xsd:string" />   </wsdl:message>- <wsdl:message name="getBalanceRequest">  <wsdl:part name="username" type="xsd:string" />   <wsdl:part name="pwd" type="xsd:string" />   </wsdl:message>- <wsdl:message name="sendSmsRequest1">  <wsdl:part name="username" type="xsd:string" />   <wsdl:part name="pwd" type="xsd:string" />   <wsdl:part name="gwid" type="xsd:string" />   <wsdl:part name="phone" type="xsd:string" />   <wsdl:part name="content" type="xsd:string" />   <wsdl:part name="npwd" type="xsd:string" />   <wsdl:part name="page" type="xsd:string" />   <wsdl:part name="cid" type="xsd:string" />   </wsdl:message>- <wsdl:message name="getReportRequest">  <wsdl:part name="username" type="xsd:string" />   <wsdl:part name="pwd" type="xsd:string" />   </wsdl:message>- <wsdl:message name="getMoRequest1">  <wsdl:part name="username" type="xsd:string" />   <wsdl:part name="pwd" type="xsd:string" />   <wsdl:part name="version" type="xsd:string" />   <wsdl:part name="number" type="xsd:string" />   <wsdl:part name="content" type="xsd:string" />   <wsdl:part name="npwd" type="xsd:string" />   <wsdl:part name="page" type="xsd:string" />   <wsdl:part name="cid" type="xsd:string" />   </wsdl:message>- <wsdl:message name="getReportRequest1">  <wsdl:part name="username" type="xsd:string" />   <wsdl:part name="pwd" type="xsd:string" />   <wsdl:part name="version" type="xsd:string" />   <wsdl:part name="number" type="xsd:string" />   <wsdl:part name="content" type="xsd:string" />   <wsdl:part name="npwd" type="xsd:string" />   <wsdl:part name="page" type="xsd:string" />   <wsdl:part name="cid" type="xsd:string" />   </wsdl:message>  <wsdl:message name="mainResponse" /> - <wsdl:message name="mainRequest">  <wsdl:part name="s" type="impl:ArrayOf_xsd_string" />   </wsdl:message>- <wsdl:message name="sendMmsResponse">  <wsdl:part name="sendMmsReturn" type="xsd:string" />   </wsdl:message>- <wsdl:message name="getMoRequest">  <wsdl:part name="username" type="xsd:string" />   <wsdl:part name="pwd" type="xsd:string" />   </wsdl:message>- <wsdl:message name="getBalanceResponse">  <wsdl:part name="getBalanceReturn" type="xsd:string" />   </wsdl:message>- <wsdl:message name="listSortedFilesRequest">  <wsdl:part name="dirFile" type="xsd:anyType" />   </wsdl:message>- <wsdl:message name="getReportResponse">  <wsdl:part name="getReportReturn" type="xsd:string" />   </wsdl:message>- <wsdl:message name="getMoResponse1">  <wsdl:part name="getMoReturn" type="xsd:string" />   </wsdl:message>- <wsdl:message name="sendSmsResponse1">  <wsdl:part name="sendSmsReturn" type="xsd:string" />   </wsdl:message>- <wsdl:message name="getBalanceRequest1">  <wsdl:part name="username" type="xsd:string" />   <wsdl:part name="pwd" type="xsd:string" />   <wsdl:part name="version" type="xsd:string" />   <wsdl:part name="number" type="xsd:string" />   <wsdl:part name="content" type="xsd:string" />   <wsdl:part name="npwd" type="xsd:string" />   <wsdl:part name="page" type="xsd:string" />   <wsdl:part name="cid" type="xsd:string" />   </wsdl:message>- <wsdl:message name="sendMmsRequest">  <wsdl:part name="username" type="xsd:string" />   <wsdl:part name="pwd" type="xsd:string" />   <wsdl:part name="phone" type="xsd:string" />   <wsdl:part name="title" type="xsd:string" />   <wsdl:part name="content" type="xsd:string" />   <wsdl:part name="gwid" type="xsd:string" />   </wsdl:message>- <wsdl:message name="smsFileNameResponse">  <wsdl:part name="smsFileNameReturn" type="xsd:string" />   </wsdl:message>- <wsdl:portType name="SmsService">- <wsdl:operation name="main" parameterOrder="s">  <wsdl:input message="impl:mainRequest" name="mainRequest" />   <wsdl:output message="impl:mainResponse" name="mainResponse" />   </wsdl:operation>- <wsdl:operation name="getBalance" parameterOrder="username pwd">  <wsdl:input message="impl:getBalanceRequest" name="getBalanceRequest" />   <wsdl:output message="impl:getBalanceResponse" name="getBalanceResponse" />   </wsdl:operation>- <wsdl:operation name="getBalance" parameterOrder="username pwd version number content npwd page cid">  <wsdl:input message="impl:getBalanceRequest1" name="getBalanceRequest1" />   <wsdl:output message="impl:getBalanceResponse1" name="getBalanceResponse1" />   </wsdl:operation>- <wsdl:operation name="sendSms" parameterOrder="username pwd phone content gwid">  <wsdl:input message="impl:sendSmsRequest" name="sendSmsRequest" />   <wsdl:output message="impl:sendSmsResponse" name="sendSmsResponse" />   </wsdl:operation>- <wsdl:operation name="sendSms" parameterOrder="username pwd gwid phone content npwd page cid">  <wsdl:input message="impl:sendSmsRequest1" name="sendSmsRequest1" />   <wsdl:output message="impl:sendSmsResponse1" name="sendSmsResponse1" />   </wsdl:operation>- <wsdl:operation name="smsFileName" parameterOrder="tag">  <wsdl:input message="impl:smsFileNameRequest" name="smsFileNameRequest" />   <wsdl:output message="impl:smsFileNameResponse" name="smsFileNameResponse" />   </wsdl:operation>- <wsdl:operation name="sendMms" parameterOrder="username pwd phone title content gwid">  <wsdl:input message="impl:sendMmsRequest" name="sendMmsRequest" />   <wsdl:output message="impl:sendMmsResponse" name="sendMmsResponse" />   </wsdl:operation>- <wsdl:operation name="getReport" parameterOrder="username pwd">  <wsdl:input message="impl:getReportRequest" name="getReportRequest" />   <wsdl:output message="impl:getReportResponse" name="getReportResponse" />   </wsdl:operation>- <wsdl:operation name="getReport" parameterOrder="username pwd version number content npwd page cid">  <wsdl:input message="impl:getReportRequest1" name="getReportRequest1" />   <wsdl:output message="impl:getReportResponse1" name="getReportResponse1" />   </wsdl:operation>- <wsdl:operation name="listSortedFiles" parameterOrder="dirFile">  <wsdl:input message="impl:listSortedFilesRequest" name="listSortedFilesRequest" />   <wsdl:output message="impl:listSortedFilesResponse" name="listSortedFilesResponse" />   </wsdl:operation>- <wsdl:operation name="getMo" parameterOrder="username pwd">  <wsdl:input message="impl:getMoRequest" name="getMoRequest" />   <wsdl:output message="impl:getMoResponse" name="getMoResponse" />   </wsdl:operation>- <wsdl:operation name="getMo" parameterOrder="username pwd version number content npwd page cid">  <wsdl:input message="impl:getMoRequest1" name="getMoRequest1" />   <wsdl:output message="impl:getMoResponse1" name="getMoResponse1" />   </wsdl:operation>  </wsdl:portType>- <wsdl:binding name="SmsServiceSoapBinding" type="impl:SmsService">  <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> - <wsdl:operation name="main">  <wsdlsoap:operation soapAction="" /> - <wsdl:input name="mainRequest">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://util.sms.jtd.com" use="encoded" />   </wsdl:input>- <wsdl:output name="mainResponse">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://短信服务器IP/qxt_jtd/service/SmsService" use="encoded" />   </wsdl:output>  </wsdl:operation>- <wsdl:operation name="getBalance">  <wsdlsoap:operation soapAction="" /> - <wsdl:input name="getBalanceRequest">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://util.sms.jtd.com" use="encoded" />   </wsdl:input>- <wsdl:output name="getBalanceResponse">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://短信服务器IP/qxt_jtd/service/SmsService" use="encoded" />   </wsdl:output>  </wsdl:operation>- <wsdl:operation name="getBalance">  <wsdlsoap:operation soapAction="" /> - <wsdl:input name="getBalanceRequest1">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://util.sms.jtd.com" use="encoded" />   </wsdl:input>- <wsdl:output name="getBalanceResponse1">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://短信服务器IP/qxt_jtd/service/SmsService" use="encoded" />   </wsdl:output>  </wsdl:operation>- <wsdl:operation name="sendSms">  <wsdlsoap:operation soapAction="" /> - <wsdl:input name="sendSmsRequest">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://util.sms.jtd.com" use="encoded" />   </wsdl:input>- <wsdl:output name="sendSmsResponse">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://短信服务器IP/qxt_jtd/service/SmsService" use="encoded" />   </wsdl:output>  </wsdl:operation>- <wsdl:operation name="sendSms">  <wsdlsoap:operation soapAction="" /> - <wsdl:input name="sendSmsRequest1">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://util.sms.jtd.com" use="encoded" />   </wsdl:input>- <wsdl:output name="sendSmsResponse1">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://短信服务器IP/qxt_jtd/service/SmsService" use="encoded" />   </wsdl:output>  </wsdl:operation>- <wsdl:operation name="smsFileName">  <wsdlsoap:operation soapAction="" /> - <wsdl:input name="smsFileNameRequest">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://util.sms.jtd.com" use="encoded" />   </wsdl:input>- <wsdl:output name="smsFileNameResponse">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://短信服务器IP/qxt_jtd/service/SmsService" use="encoded" />   </wsdl:output>  </wsdl:operation>- <wsdl:operation name="sendMms">  <wsdlsoap:operation soapAction="" /> - <wsdl:input name="sendMmsRequest">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://util.sms.jtd.com" use="encoded" />   </wsdl:input>- <wsdl:output name="sendMmsResponse">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://短信服务器IP/qxt_jtd/service/SmsService" use="encoded" />   </wsdl:output>  </wsdl:operation>- <wsdl:operation name="getReport">  <wsdlsoap:operation soapAction="" /> - <wsdl:input name="getReportRequest">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://util.sms.jtd.com" use="encoded" />   </wsdl:input>- <wsdl:output name="getReportResponse">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://短信服务器IP/qxt_jtd/service/SmsService" use="encoded" />   </wsdl:output>  </wsdl:operation>- <wsdl:operation name="getReport">  <wsdlsoap:operation soapAction="" /> - <wsdl:input name="getReportRequest1">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://util.sms.jtd.com" use="encoded" />   </wsdl:input>- <wsdl:output name="getReportResponse1">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://短信服务器IP/qxt_jtd/service/SmsService" use="encoded" />   </wsdl:output>  </wsdl:operation>- <wsdl:operation name="listSortedFiles">  <wsdlsoap:operation soapAction="" /> - <wsdl:input name="listSortedFilesRequest">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://util.sms.jtd.com" use="encoded" />   </wsdl:input>- <wsdl:output name="listSortedFilesResponse">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://短信服务器IP/qxt_jtd/service/SmsService" use="encoded" />   </wsdl:output>  </wsdl:operation>- <wsdl:operation name="getMo">  <wsdlsoap:operation soapAction="" /> - <wsdl:input name="getMoRequest">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://util.sms.jtd.com" use="encoded" />   </wsdl:input>- <wsdl:output name="getMoResponse">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://短信服务器IP/qxt_jtd/service/SmsService" use="encoded" />   </wsdl:output>  </wsdl:operation>- <wsdl:operation name="getMo">  <wsdlsoap:operation soapAction="" /> - <wsdl:input name="getMoRequest1">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://util.sms.jtd.com" use="encoded" />   </wsdl:input>- <wsdl:output name="getMoResponse1">  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://短信服务器IP/qxt_jtd/service/SmsService" use="encoded" />   </wsdl:output>  </wsdl:operation>  </wsdl:binding>- <wsdl:service name="SmsServiceService">- <wsdl:port binding="impl:SmsServiceSoapBinding" name="SmsService">  <wsdlsoap:address location="http://短信服务器IP/qxt_jtd/service/SmsService" />   </wsdl:port>  </wsdl:service>  </wsdl:definitions>

2、其次我们来研究几个需要关注的信息:

目标命名空间[targetNamespace]:http://短信服务器IP/qxt_jtd/service/SmsService,你可以Ctrl+F在上面查找该属性的值

WebService服务的名字、端点名字、端点地址:让我们看下面这个节点:

- <wsdl:service name="SmsServiceService">- <wsdl:port binding="impl:SmsServiceSoapBinding" name="SmsService">  <wsdlsoap:address location="http://短信服务器IP/qxt_jtd/service/SmsService" />   </wsdl:port>  </wsdl:service>
在上面的信息中:
name="SmsServiceService"----------------->表示服务的名字name="SmsService"------------------------>表示端点的名字location="http://短信服务器IP/qxt_jtd/service/SmsService"---->表示端点的地址
3、这个端点下有哪些方法可以调用,方法的参数和返回值分别是什么呢,参考下面的节点信息:
- <wsdl:portType name="SmsService">- <wsdl:operation name="main" parameterOrder="s">  <wsdl:input message="impl:mainRequest" name="mainRequest" />   <wsdl:output message="impl:mainResponse" name="mainResponse" />   </wsdl:operation>- <wsdl:operation name="getBalance" parameterOrder="username pwd">  <wsdl:input message="impl:getBalanceRequest" name="getBalanceRequest" />   <wsdl:output message="impl:getBalanceResponse" name="getBalanceResponse" />   </wsdl:operation>- <wsdl:operation name="getBalance" parameterOrder="username pwd version number content npwd page cid">  <wsdl:input message="impl:getBalanceRequest1" name="getBalanceRequest1" />   <wsdl:output message="impl:getBalanceResponse1" name="getBalanceResponse1" />   </wsdl:operation>- <wsdl:operation name="sendSms" parameterOrder="username pwd phone content gwid">  <wsdl:input message="impl:sendSmsRequest" name="sendSmsRequest" />   <wsdl:output message="impl:sendSmsResponse" name="sendSmsResponse" />   </wsdl:operation>- <wsdl:operation name="sendSms" parameterOrder="username pwd gwid phone content npwd page cid">  <wsdl:input message="impl:sendSmsRequest1" name="sendSmsRequest1" />   <wsdl:output message="impl:sendSmsResponse1" name="sendSmsResponse1" />   </wsdl:operation>- <wsdl:operation name="smsFileName" parameterOrder="tag">  <wsdl:input message="impl:smsFileNameRequest" name="smsFileNameRequest" />   <wsdl:output message="impl:smsFileNameResponse" name="smsFileNameResponse" />   </wsdl:operation>- <wsdl:operation name="sendMms" parameterOrder="username pwd phone title content gwid">  <wsdl:input message="impl:sendMmsRequest" name="sendMmsRequest" />   <wsdl:output message="impl:sendMmsResponse" name="sendMmsResponse" />   </wsdl:operation>- <wsdl:operation name="getReport" parameterOrder="username pwd">  <wsdl:input message="impl:getReportRequest" name="getReportRequest" />   <wsdl:output message="impl:getReportResponse" name="getReportResponse" />   </wsdl:operation>- <wsdl:operation name="getReport" parameterOrder="username pwd version number content npwd page cid">  <wsdl:input message="impl:getReportRequest1" name="getReportRequest1" />   <wsdl:output message="impl:getReportResponse1" name="getReportResponse1" />   </wsdl:operation>- <wsdl:operation name="listSortedFiles" parameterOrder="dirFile">  <wsdl:input message="impl:listSortedFilesRequest" name="listSortedFilesRequest" />   <wsdl:output message="impl:listSortedFilesResponse" name="listSortedFilesResponse" />   </wsdl:operation>- <wsdl:operation name="getMo" parameterOrder="username pwd">  <wsdl:input message="impl:getMoRequest" name="getMoRequest" />   <wsdl:output message="impl:getMoResponse" name="getMoResponse" />   </wsdl:operation>- <wsdl:operation name="getMo" parameterOrder="username pwd version number content npwd page cid">  <wsdl:input message="impl:getMoRequest1" name="getMoRequest1" />   <wsdl:output message="impl:getMoResponse1" name="getMoResponse1" />   </wsdl:operation>  </wsdl:portType>


以发送短消息为例:

<wsdl:operation name="sendSms" parameterOrder="username pwd phone content gwid">  <wsdl:input message="impl:sendSmsRequest" name="sendSmsRequest" />   <wsdl:output message="impl:sendSmsResponse" name="sendSmsResponse" />   </wsdl:operation>

方法名称:sendSms;

方法参数:username pwd phone content gwid;

方法返回值类型参见WSDL关于消息“sendSmsResponse”的描述:

<wsdl:message name="smsFileNameResponse">
  <wsdl:part name="smsFileNameReturn" type="xsd:string" />
  </wsdl:message>
说明是返回值是字串类型。


4、使用JAX-WS调用这个WebService中的方法:

发送消息示范:

package sms;import java.net.*;import java.util.*;import javax.xml.*;import javax.xml.soap.*;import javax.xml.ws.*;import javax.xml.ws.soap.*;import javax.xml.namespace.*;public class SendClient {// 名字空间public static final String targetNamespace = "http://短信服务器IP/qxt_jtd/service/SmsService";// 服务名public static final String serName = "SmsServiceService";// 端口名public static final String pName = "SmsService";// 服务地址public static final String endpointAddress = "http://短信服务器IP/qxt_jtd/service/SmsService";// 方法名public static final String OPER_NAME = "sendSms";static Object[] myargs = new Object[5];public static void main(String[] args) throws MalformedURLException,Exception {myargs[0] = "签约企业短信账号";myargs[1] = "签约企业短信密码";myargs[2] = "发送的目标手机号码1,目标手机号2,目标手机号码3";myargs[3] = "短信内容";myargs[4] = "短信通道类型";//构造服务名对象QName serviceName = new QName(targetNamespace, serName);//构造端口名对象QName portName = new QName(targetNamespace, pName);//通过服务名对象创建服务对象javax.xml.ws.Service service = Service.create(serviceName);//向服务上添加一个新的端口service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING,endpointAddress);//创建一个分发实例Dispatch<SOAPMessage> dispatch = service.createDispatch(portName,SOAPMessage.class, Service.Mode.MESSAGE);BindingProvider bp = (BindingProvider)dispatch;//获得请求上下文用于初始化请求信息Map<String, Object> rc = bp.getRequestContext();rc.put(BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);rc.put(BindingProvider.SOAPACTION_URI_PROPERTY, OPER_NAME);MessageFactory factory = ((SOAPBinding) bp.getBinding()).getMessageFactory();SOAPMessage request = factory.createMessage();SOAPBody body = request.getSOAPBody();QName payloadName = new QName(targetNamespace, OPER_NAME, "ns1");SOAPBodyElement payload = body.addBodyElement(payloadName);SOAPElement msg1 = payload.addChildElement("username");msg1.addTextNode(myargs[0].toString());SOAPElement msg2 = payload.addChildElement("pwd");msg2.addTextNode(myargs[1].toString());SOAPElement msg3 = payload.addChildElement("phone");msg3.addTextNode(myargs[2].toString());SOAPElement msg4 = payload.addChildElement("content");msg4.addTextNode(myargs[3].toString());SOAPElement msg5 = payload.addChildElement("gwid");msg5.addTextNode(myargs[4].toString());SOAPMessage reply = null;try {reply = dispatch.invoke(request);} catch (WebServiceException wse) {wse.printStackTrace();}SOAPBody soapBody = reply.getSOAPBody();SOAPBodyElement nextSoapBodyElement = (SOAPBodyElement) soapBody.getChildElements().next();SOAPElement soapElement = (SOAPElement) nextSoapBodyElement.getChildElements().next();System.out.println("获取回应信息为:" + soapElement.getValue());}}


接收消息示范:[获得手机回复的短信]

package sms;import java.net.*;import java.util.*;import javax.xml.*;import javax.xml.soap.*;import javax.xml.ws.*;import javax.xml.ws.soap.*;import javax.xml.namespace.*;public class ReceiveClient {// 名字空间public static final String targetNamespace = "http://短信服务器IP/qxt_jtd/service/SmsService";// 服务名public static final String serName = "SmsServiceService";// 端口名public static final String pName = "SmsService";// 服务地址public static final String endpointAddress = "http://短信服务器IP/qxt_jtd/service/SmsService";// 方法名public static final String OPER_NAME = "getMo";static Object[] myargs = new Object[2];public static void main(String[] args) throws MalformedURLException,Exception {
myargs[0] = "签约企业短信账号";myargs[1] = "签约企业短信密码";
QName serviceName = new QName(targetNamespace, serName);QName portName = new QName(targetNamespace, pName);javax.xml.ws.Service service = Service.create(serviceName);service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING,endpointAddress);Dispatch<SOAPMessage> dispatch = service.createDispatch(portName,SOAPMessage.class, Service.Mode.MESSAGE);BindingProvider bp = (BindingProvider) dispatch;Map<String, Object> rc = bp.getRequestContext();rc.put(BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);rc.put(BindingProvider.SOAPACTION_URI_PROPERTY, OPER_NAME);MessageFactory factory = ((SOAPBinding) bp.getBinding()).getMessageFactory();SOAPMessage request = factory.createMessage();SOAPBody body = request.getSOAPBody();QName payloadName = new QName(targetNamespace, OPER_NAME,XMLConstants.DEFAULT_NS_PREFIX);SOAPBodyElement payload = body.addBodyElement(payloadName);SOAPElement msg1 = payload.addChildElement("username");msg1.addTextNode(myargs[0].toString());SOAPElement msg2 = payload.addChildElement("pwd");msg2.addTextNode(myargs[1].toString());SOAPMessage reply = null;try {reply = dispatch.invoke(request);} catch (WebServiceException wse) {wse.printStackTrace();}SOAPBody soapBody = reply.getSOAPBody();SOAPBodyElement nextSoapBodyElement = (SOAPBodyElement) soapBody.getChildElements().next();SOAPElement soapElement = (SOAPElement) nextSoapBodyElement.getChildElements().next();String v = soapElement.getValue();v = java.net.URLDecoder.decode(v, "UTF-8");System.out.println("获取回应信息为:" + v);}}

关于如下使用JAX-WS创建发布和调用WebService请参考:http://fusesource.com/docs/framework/2.2/jaxws/index.html

	
				
		
原创粉丝点击