一、webservice 服务的调用。
1 service提供的wsdl为:根据wsdl2java包进行生成java代码。
<?xml version="1.0" encoding="UTF-8"?> <definitions name="Party3" targetNamespace="http://interface.i2b.cn:8801" xmlns:tns="http://interface.i2b.cn:8801" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://interface.i2b.cn:8801" xmlns:SOAP="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:MIME="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:DIME="http://schemas.xmlsoap.org/ws/2002/04/dime/wsdl/" xmlns:WSDL="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"> <types> <schema targetNamespace="http://interface.i2b.cn:8801" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://interface.i2b.cn:8801" xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified"> <import namespace="http://schemas.xmlsoap.org/soap/encoding/" schemaLocation="http://schemas.xmlsoap.org/soap/encoding/"/> <complexType name="UsrInfoV1"> <sequence> <element name="name" type="xsd:string" minOccurs="1" maxOccurs="1"/> <element name="password" type="xsd:string" minOccurs="1" maxOccurs="1"/> </sequence> </complexType> <complexType name="SmsInfoV1"> <sequence> <element name="sendNum" type="xsd:string" minOccurs="1" maxOccurs="1"/> <element name="recvNum" type="xsd:string" minOccurs="1" maxOccurs="1"/> <element name="sendDat" type="xsd:string" minOccurs="1" maxOccurs="1"/> </sequence> </complexType> </schema> </types> <message name="SmsStatV1"> <part name="usr" type="ns:UsrInfoV1"/> </message> <message name="UsrStatV1"> <part name="ret" type="xsd:int"/> <part name="total" type="xsd:int"/> <part name="send" type="xsd:int"/> <part name="remain" type="xsd:int"/> </message> <message name="SmsSendV1Request"> <part name="usr" type="ns:UsrInfoV1"/> <part name="sms" type="ns:SmsInfoV1"/> </message> <message name="SmsSendV1Response"> <part name="ret" type="xsd:int"/> </message> <portType name="SmsParty3"> <operation name="SmsStatV1"> <documentation>Service definition of function ns__SmsStatV1</documentation> <input message="tns:SmsStatV1"/> <output message="tns:UsrStatV1"/> </operation> <operation name="SmsSendV1"> <documentation>Service definition of function ns__SmsSendV1</documentation> <input message="tns:SmsSendV1Request"/> <output message="tns:SmsSendV1Response"/> </operation> </portType> <binding name="Party3" type="tns:SmsParty3"> <SOAP:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="SmsStatV1"> <SOAP:operation style="rpc" soapAction=""/> <input> <SOAP:body use="encoded" namespace="http://interface.i2b.cn:8801" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </input> <output> <SOAP:body use="encoded" namespace="http://interface.i2b.cn:8801" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </output> </operation> <operation name="SmsSendV1"> <SOAP:operation style="rpc" soapAction=""/> <input> <SOAP:body use="encoded" namespace="http://interface.i2b.cn:8801" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </input> <output> <SOAP:body use="encoded" namespace="http://interface.i2b.cn:8801" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </output> </operation> </binding> <service name="Party3"> <documentation>service definition</documentation> <port name="Party3" binding="tns:Party3"> <SOAP:address location="http://interface.i2b.cn:8801"/> </port> </service> </definitions>
2 配置java环境,进入doc窗口,再找到wsdl2java包所在位置。运行下面命令
Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java Party3.wsdl -p com.company.function
3 生成的java代码为:(图片不会上传)
4 调用:
Party3_Service service = new Party3_ServiceLocator();
Party3Stub binding = (Party3Stub) service.getParty3(new URL("http://xxx:8801"));
binding.smsStatV1(xx,xx,xxx)
二 调用http协议的服务
package sms;
import java.io.*;
import java.net.*;
public class SendSMSClient
{
public static String url = "http://****/SMS";//宽乐短信 接口地址
public static String uid = "******";//UC 帐号
public static String psw = "******";//UC 密码
public static String SendSms(String mobiles,String msgid,String content)
{
String ret="";
try
{
String msg = java.net.URLEncoder.encode(content, "GB2312");
String theUrl = url + "?cmd=send&uid=" + uid + "&psw=" + psw + "&mobiles=13590234458&msgid=" + msgid + "&msg=" + msg;
ret = getUrlData(theUrl, 50);
}
catch(Exception ex)
{
ex.printStackTrace();
}
return ret;
}
public static String GetMo()
{
String ret="";
try
{
String theUrl = url + "?cmd=getmo&uid=" + uid + "&psw=" + psw;
ret = getUrlData(theUrl, 50);
}
catch(Exception ex)
{
}
return ret;
}
public static String getUrlData(String s, int i)
{
try
{
URL url = new URL(s);
URLConnection urlconnection = url.openConnection();
urlconnection.setUseCaches(false);
urlconnection.connect();
InputStream inputstream = urlconnection.getInputStream();
byte abyte0[] = new byte[i];
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
int j;
do
{
j = inputstream.read(abyte0, 0, i);
if (j != -1)
{
bytearrayoutputstream.write(abyte0, 0, j);
}
}
while (j != -1);
String s1 = new String(bytearrayoutputstream.toByteArray());
inputstream.close();
return s1;
}
catch (Exception ex)
{
ex.printStackTrace();
}
return null;
}
public static void main(String args[])
{
//String retSend=SendSms("13590234458","10001","测试发送");
//System.out.println("send ret="+retSend);
//String retGetMo=GetMo();
//System.out.println("getmo ret="+retGetMo);
}
}