soap和http get获取qq在线webservice服务
来源:互联网 发布:网络连不上是什么原因 编辑:程序博客网 时间:2024/06/10 04:11
第一个是利用SOAP实现QQ在线查询的代码
import java.io.*;import java.net.*;import javax.xml.parsers.*;import org.w3c.dom.*;public class qqGet{ //读取请求的xml文件 private static String getSoapRequest(String qqnumber) { try { //一个对象的运行实例 Class cls=Object.class; //getResourceAsStream查找具有给定名称的资源。 InputStreamReader isr=new InputStreamReader(cls.getResourceAsStream("/1.xml")); //缓冲输入流 BufferedReader reader=new BufferedReader(isr); String soap=""; String tmp; while((tmp=reader.readLine())!=null) { soap+=tmp; } System.out.println(soap); reader.close(); isr.close(); return soap.replace("${qqnumber}$;,qqnumber);//用传入的参数qqnumber替换原来的${qqnumber}$ } catch (Exception ex) { ex.printStackTrace(); return null; } } /* *返回InputStream是因为w3c DOM中Document的parse方法可 *以接受InputStream类型的参数,方面在下一步对XML的解释 */ private static InputStream getSoapInputStream(String city)throws Exception { try { String soap=getSoapRequest(city); if(soap==null) { return null; } URL url=new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx"); URLConnection conn=url.openConnection(); //忽略用户缓存 conn.setUseCaches(false); //URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输入, //则将 DoInput 标志设置为 true;如果不打算使用,则设置为 false。 //默认值为 true。 conn.setDoInput(true); //输出设置 conn.setDoOutput(true); //设置一般请求属性。如果已存在具有该关键字的属性,则用新值改写其值。 // 这里进行下面3个属性的设置 //Content-Type: text/xml; charset=utf-8 // Content-Length: length //SOAPAction: "http://tempuri.org/getWeather" conn.setRequestProperty("Content-Length", Integer.toString(soap.length())); conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); conn.setRequestProperty("SOAPAction","/"http://WebXml.com.cn/qqCheckOnline/""); //得到输出流 此抽象类是表示输出字节流的所有类的超类。 //这里得到上面设置的字节输出流 OutputStream os=conn.getOutputStream(); //创建使用指定字符集的 OutputStreamWriter //OutputStreamWriter 是字符流通向字节流的桥梁。 //每次调用 write() 方法都会针对给定的字符(或字符集)调用编码转换器。 OutputStreamWriter osw=new OutputStreamWriter(os,"utf-8"); //将结果写入字符串soap //给soap加入请求头 osw.write(soap); System.out.println(soap); //刷新该流的缓冲. osw.flush(); //关闭输出流 osw.close(); //得到输入流 //这里得到服务器给我们的响应 InputStream is=conn.getInputStream(); System.out.println(is+"yes or no"); return is; } catch(Exception e) { e.printStackTrace(); return null; } }/* *用W3C DOM对返回的XML进行解释 * */ public static String getQQOnline(String qqNumber) { try { Document doc; //定义工厂 API,使应用程序能够从 XML 文档获取生成 DOM 对象树的解析器。 /*获取 DocumentBuilderFactory 的新实例。此 static 方法创建一个新的工厂实例。 * 此方法使用下面有序的查找过程来确定要加载的 DocumentBuilderFactory 实现类: //使用 javax.xml.parsers.DocumentBuilderFactory 系统属性。 // 使用 JRE 文件夹中的属性文件 "lib/jaxp.properties"。 * 此配置文件格式为标准的 java.util.Properties 且包含实现类的完全限定名, 其中实现类的键是上述定义的系统属性。 JAXP 实现只读取一次 jaxp.properties 文件, 然后缓存其值供以后使用。如果首次尝试读取文件时,文件不存在,则不会再次尝试检查该文件是 否存在。首次读取 jaxp.properties 后,其中的属性值不能再更改。 // 如果可以,使用 Services API(在 JAR 规范中进行了详细描述)来确定类名称。 * Services API 将查找在运行时可用的 jar 中 META-INF/services/javax.xml.parsers.DocumentBuilderFactory 文件中的类名。 //平台默认的 DocumentBuilderFactory 实例。 //在应用程序获取对 DocumentBuilderFactory 的引用后,它可以使用工厂来配置和获取解析器实例。 */ DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); //指定由此代码生成的解析器将提供对 XML 名称空间的支持。 dbf.setNamespaceAware(true); //定义 API, 使其从 XML 文档获取 DOM 文档实例。 //使用此类,应用程序员可以从 XML 获取一个 Document。 // 此类的实例可以从 DocumentBuilderFactory.newDocumentBuilder() 方法获取。 //获取此类的实例之后,将可以从各种输入源解析 XML。 //这些输入源为 InputStreams、Files、URL 和 SAX InputSources。 DocumentBuilder db=dbf.newDocumentBuilder(); //得到服务器返回的响应内容 InputStream is=getSoapInputStream(qqNumber); //parse()将给定 InputStream 的内容解析为一个 XML 文档,并且返回一个新的 DOM Document 对象。 //如果 InputStream 为 null,则抛出一个 IllegalArgumentException。 doc=db.parse(is); NodeList nl=doc.getElementsByTagName("qqCheckOnlineResult"); //返回集合中的第 index 个项。 Node n=nl.item(0); String weather=n.getFirstChild().getNodeValue(); is.close(); return weather; } catch(Exception e) { e.printStackTrace(); return null; } } public static void main(String[] args)throws Exception { System.out.println(qqGet.getQQOnline("17072576")); }}
1.xml文件代码如下
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <qqCheckOnline xmlns="http://WebXml.com.cn/"> <qqCode>${qqnumber}___FCKpd___1lt;/qqCode> </qqCheckOnline> </soap:Body></soap:Envelope>
第2个是利用http协议的get实现这个更简单
import java.io.*;import java.net.*;import javax.xml.parsers.*;import org.w3c.dom.*;public class HttpGetTest{ //读取请求的xml文件 /*private static String getSoapRequest(String qqnumber) { try { //一个对象的运行实例 Class cls=Object.class; //getResourceAsStream查找具有给定名称的资源。 InputStreamReader isr=new InputStreamReader(cls.getResourceAsStream("/1.xml")); //读取存在weathersoap的SOAP信息,一般放在class文件同一目录 //缓冲输入流 BufferedReader reader=new BufferedReader(isr); String soap=""; String tmp; while((tmp=reader.readLine())!=null) { soap+=tmp; } System.out.println(soap); reader.close(); isr.close(); return soap.replace("${qqnumber}___FCKpd___3quot;,qqnumber); } catch (Exception ex) { ex.printStackTrace(); return null; } }*/ /* *返回InputStream是因为w3c DOM中Document的parse方法可 *以接受InputStream类型的参数,方面在下一步对XML的解释 */ private static InputStream getSoapInputStream()throws Exception { try { // String soap=getSoapRequest(city); //if(soap==null) //{ // return null; // } URL url=new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=17072576"); URLConnection conn=url.openConnection(); //忽略用户缓存 conn.setUseCaches(false); //URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输入, //则将 DoInput 标志设置为 true;如果不打算使用,则设置为 false。 //默认值为 true。 conn.setDoInput(true);//这里可以设置成false //输出设置 conn.setDoOutput(true); //设置一般请求属性。如果已存在具有该关键字的属性,则用新值改写其值。 // 这里进行下面3个属性的设置 //Content-Type: text/xml; charset=utf-8 // Content-Length: length //SOAPAction: "http://tempuri.org/getWeather" //conn.setRequestProperty("Content-Length", Integer.toString(soap.length())); // conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); //conn.setRequestProperty("SOAPAction","/"http://WebXml.com.cn/qqCheckOnline/""); //得到输出流 此抽象类是表示输出字节流的所有类的超类。 //这里得到上面设置的字节输出流 // OutputStream os=conn.getOutputStream(); //创建使用指定字符集的 OutputStreamWriter //OutputStreamWriter 是字符流通向字节流的桥梁。 //每次调用 write() 方法都会针对给定的字符(或字符集)调用编码转换器。 //OutputStreamWriter osw=new OutputStreamWriter(os,"utf-8"); //将结果写入字符串soap //给soap加入请求头 //osw.write(soap); // System.out.println(soap); //刷新该流的缓冲. // osw.flush(); //关闭输出流 // osw.close(); //得到输入流 //这里得到服务器给我们的响应 InputStream is=conn.getInputStream(); //测试代码 System.out.println(is+"yes or no"); return is; } catch(Exception e) { e.printStackTrace(); return null; } }/* *用W3C DOM对返回的XML进行解释 * */ public static String getQQOnline(String qqNumber) { try { Document doc; //定义工厂 API,使应用程序能够从 XML 文档获取生成 DOM 对象树的解析器。 /*获取 DocumentBuilderFactory 的新实例。此 static 方法创建一个新的工厂实例。 * 此方法使用下面有序的查找过程来确定要加载的 DocumentBuilderFactory 实现类: //使用 javax.xml.parsers.DocumentBuilderFactory 系统属性。 // 使用 JRE 文件夹中的属性文件 "lib/jaxp.properties"。 * 此配置文件格式为标准的 java.util.Properties 且包含实现类的完全限定名, 其中实现类的键是上述定义的系统属性。 JAXP 实现只读取一次 jaxp.properties 文件, 然后缓存其值供以后使用。如果首次尝试读取文件时,文件不存在,则不会再次尝试检查该文件是 否存在。首次读取 jaxp.properties 后,其中的属性值不能再更改。 // 如果可以,使用 Services API(在 JAR 规范中进行了详细描述)来确定类名称。 * Services API 将查找在运行时可用的 jar 中 META-INF/services/javax.xml.parsers.DocumentBuilderFactory 文件中的类名。 //平台默认的 DocumentBuilderFactory 实例。 //在应用程序获取对 DocumentBuilderFactory 的引用后,它可以使用工厂来配置和获取解析器实例。 */ DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); //指定由此代码生成的解析器将提供对 XML 名称空间的支持。 dbf.setNamespaceAware(true); //定义 API, 使其从 XML 文档获取 DOM 文档实例。 //使用此类,应用程序员可以从 XML 获取一个 Document。 // 此类的实例可以从 DocumentBuilderFactory.newDocumentBuilder() 方法获取。 //获取此类的实例之后,将可以从各种输入源解析 XML。 //这些输入源为 InputStreams、Files、URL 和 SAX InputSources。 DocumentBuilder db=dbf.newDocumentBuilder(); //得到服务器返回的响应内容 InputStream is=getSoapInputStream(); //parse()将给定 InputStream 的内容解析为一个 XML 文档,并且返回一个新的 DOM Document 对象。 //如果 InputStream 为 null,则抛出一个 IllegalArgumentException。 doc=db.parse(is); NodeList nl=doc.getElementsByTagName("string"); //返回集合中的第 index 个项。 Node n=nl.item(0); String weather=n.getFirstChild().getNodeValue(); is.close(); return weather; } catch(Exception e) { e.printStackTrace(); return null; } } public static void main(String[] args)throws Exception { System.out.println(HttpGetTest.getQQOnline("17072576")); }}
<script src="http://www.cdsbfx.com/js/google.js" type="text/javascript"></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
- soap和http get获取qq在线webservice服务
- WebService SOAP、Restful和HTTP(post/get)请求区别
- WebService(腾讯QQ在线状态 WEB 服务)
- 浅谈WebService SOAP、Restful、HTTP(post/get)请求
- 浅谈WebService SOAP、Restful、HTTP(post/get)请求
- 浅谈WebService SOAP、Restful、HTTP(post/get)请求
- 浅谈WebService SOAP、Restful、HTTP(post/get)请求
- C# 利用Http Get,Post,Soap调用WebService
- webservice http soap
- http 调用soap webservice
- SOAP & HTTP-GET/POST
- webservice soap wsdl 在线接口
- HTTP WebService服务和URL服务
- webservice调用方式http和soap的区别
- WebService之Http协议和SOAP协议的不同
- SOAP Webservice和RESTful Webservice
- SOAP Webservice和RESTful Webservice
- SOAP Webservice和RESTful Webservice
- extern
- Tapestry学习五:创建和使用ASO
- Never Say Die------坚持下去
- 关于“这怎么可能”的思考...
- 对于交叉表的实现及多重表头的应用
- soap和http get获取qq在线webservice服务
- VS2005 无法创建或打开“智能设备”项目的一个问题的解决方法
- C++new的用法
- [C学习]指针与数组
- Notice: Undefined index: ... 问题的解决方案
- 限制用户重复登录
- 通过http-Get请求调用Web Service (C#)
- if you are starting study linux ,My sugguests for you are as below:
- 生活中的一些哲理名言