购买服务 会员注册 会员登录
  设为首页 加入收藏 联系我们
Welcome to WebXml.com.cn
Web Services 网站建设 域名交易 服务支持 下载中心 信息资讯 联系我们 返回首页
 
服务支持 - Web Services

如何用 JAVA 访问 WEB Service (QQ在线状态)

www.WebXml.com.cn 发表日期:2008年08月14日 12:17 星期四
 

 

最近在学习Web Service,发现了一个国内的Web Service提供站点,其中最简单的是查询QQ在线状态服务。我通过Java直接发送SOAP请求文件访问Web Service成功,这种方式实现比较简单,不需要第三方的软件包。


import java.io.*;
import java.net.*;

class QQOnlineService {
public static void main(String[] args) throws Exception {
String urlString = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx";
String xmlFile = "QQOnlineService.XML";
String soapActionString = "http://WebXml.com.cn/qqCheckOnline";

URL url = new URL(urlString);

HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();;

File fileToSend=new File(xmlFile);
byte[] buf=new byte[(int)fileToSend.length()];
new FileInputStream(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);
OutputStream out = httpConn.getOutputStream();
out.write( buf );
out.close();

InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(),"utf-8");
BufferedReader in = new BufferedReader(isr);

String inputLine;
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("result.xml")));
while ((inputLine = in.readLine()) != null){
System.out.println(inputLine);
bw.write(inputLine);
bw.newLine();
}
bw.close();
in.close();
}
}


程序用到的 QQOnlineService.XML文件可以通过预先访问http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx得到。

查询结果文件如下,对其进一步编程可以实现更为灵活的查询功能。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<qqCheckOnlineResponse xmlns="http://WebXml.com.cn/">
<qqCheckOnlineResult>N
</qqCheckOnlineResult>
</qqCheckOnlineResponse>
</soap:Body>
</soap:Envelope>



文章来源:Java大本营 > Web Service > 如何用Java访问WEB Service

 
 
 
关闭本窗口
 
请就此篇文章提供反馈:
很有帮助 没有帮助
9 8 7 6 5 4 3 2 1  
   
 
发表评论:
   
< 300中文字或< 600英文字符
 
WebXml.com.cn 声明:
  WebXml.com.cn 和/或其各供应商对于为任何目的而在本服务器上发布的文件及有关图形所含信息的适用性(不包括在单独的书面协议项下 WebXml.com.cn 向您提供的任何服务),不作任何声明。所有该等文件及有关图形均 " 依样 " 提供,而不带任何性质的保证。也许这些文件已经过期,且 WebXml.com.cn 不承诺更新它们。WebXml.com.cn 和/或其各供应商特此声明,对所有与该等信息有关的保证和条件不负任何责任,该等保证和条件包括关于使用、销售、符合特定用途、所有权和非侵权的所有默示保证和条件。在任何情况下,在由于使用或运行本服务器上的信息所引起的或与该等使用或运行有关的诉讼中,WebXml.com.cn 和/或其各供应商就任何问题而造成的损失不负任何责任。
 
 
 
WEB 服务 网站建设 域名交易 服务支持 下载中心 信息资讯 联系我们 首 页
版权所有 © 2006 - 2021 上海思集信息科技有限公司 保留所有权利        本站声明 Search powered by Google
Powered Asp.net 2.0