第一个是利用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