soap和http get获取qq在线webservice服务

第一个是利用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/"");

       //得到输出流   此抽象类是表示输出字节流的所有类的超类。

            //这里得到上面设置的字节输出流

            Ou
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值