Java使用SOAP获取webservice实例解析.docx
Java使用SOAP获取webservice实例解析
1.webservice提供方:/zh_cn/index.aspx2.下面我们以“获得腾讯QQ在线状态”为例。
参数截图如下图:
SOAP1.1
以下是SOAP1.2请求和响应示例。所显示的占位符需替换为实际值。
[/webservices/qqOnlineWebService.asmx?op=qqCheckOnline]点击前面的网址,查看对应参数信息。
3.Java程序
packagejunit;
importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.io.OutputStreamWriter;importjava.io.PrintWriter;importjava.io.UnsupportedEncodingException;.HttpURLConnection;
import.URL;
importorg.junit.Test;
publicclassJxSendSmsTest{
/**
获得腾讯QQ在线状态
*
输入参数:QQ号码String,默认QQ号码:8698053。返回数据:String,Y=在线;N=离线;E=QQ号码错误;A=商业用户验证失败;V=免费用户超过数量
@throwsException
*/
@Test
publicvoidsendSms()throwsException{StringqqCode=416501600;//qq号码StringurlString=
/webservices/qqOnlineWebService.asmx;Stringxml=
JxSendSmsTest.class.getClassLoader().getResource(SendInstantSms.xml
).getFile();
StringxmlFile=replace(xml,qqCodeTmp,qqCode).getPath();StringsoapActionString=
http://WebX/qqCheckOnline;URLurl=newURL(urlString);
HttpURLConnectionhttpConn=(HttpURLConnection)url.openConnection();FilefileToSend=newFile(xmlFile);
byte[]buf=newbyte[(int)fileToSend.length()];newFileInputStream(xmlFile).read(buf);httpConn.setRequestProperty(Content-Length,String.valueOf(buf.length));httpConn.setRequestProperty(Content-Type,text/xml;charset=utf-8);
httpConn.setRequestProperty(soapActionString,soapActionString);httpConn.setRequestMethod(POST);
httpConn.setDoOutput(true);httpConn.setDoInput(true);
OutputStreamout=httpConn.getOutputStream();out.write(buf);
out.close();
byte[]datas=readInputStream(httpConn.getInputStream());Stringresult=newString(datas);
//打印返回结果
System.out.println(result:+result);
}
/**
文件内容替换
*
@paraminFileName源文件
@param