Posted on 2009-05-18 13:57
三羽 阅读(2104)
评论(0) 编辑 收藏 所属分类:
JAVA资料
首先登录www.wopos.com/webservice/Weather.asmx?op=getWeather。可以看到如下的SOAP信息
请求:
以下内容为程序代码:
POST /webservice/Weather.asmx http/1.1
Host: www.wopos.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/getWeather"
<?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>
<getWeather xmlns="http://tempuri.org/">
<mCity>string</mCity>
</getWeather>
</soap:Body>
</soap:Envelope>
|
|
把XML部分全部复制下来创建一个XML文件(普通的文本文件也可以),为了以后编程方面,把
以下内容为程序代码:
...
<mCity>string</mCity>
...
|
|
改成
以下内容为程序代码:
...
<mCity>${city}$</mCity>
...
|
|
以weathersoap.xml保存在以后生成的类的同一目录。
响应:
以下内容为程序代码:
http/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="h ttp://www.w3.org/2001/XMLSchema" xmlns:soap="h ttp://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getWeatherResponse xmlns="h ttp://tempuri.org/">
<getWeatherResult>string</getWeatherResult>
</getWeatherResponse>
</soap:Body>
</soap:Envelope>
|
|
在后面对XML的解释要用到响应部分的XML描述
接下就开始写代码了。
以下内容为程序代码:
package jaqcy.weatherreport.client;
import java.io.*;
import java.net.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
/**
*
* @author jaqcy
*/
public class WeatherReport
{
private static String getSoapRequest(String city)//city为要查询天气的城市名
{
try
{
Class cls=Object.class;
InputStreamReader isr=new InputStreamReader(cls.getResourceAsStream("/jaqcy/weatherreport/client/weathersoap.xml"));//读取存在weathersoap的SOAP信息
BufferedReader reader=new BufferedReader(isr);
String soap="";
String tmp;
while((tmp=reader.readLine())!=null)
{
soap+=tmp;
}
reader.close();
isr.close();
return soap.replace("${city}$",city);//用传入的参数city替换原来的${city}$
}
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.wopos.com/webservice/Weather.asmx");
URLConnection conn=url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction","\"http://tempuri.org/getWeather\"");
OutputStream os=conn.getOutputStream();
OutputStreamWriter osw=new OutputStreamWriter(os,"utf-8");
osw.write(soap);
osw.flush();
osw.close();
InputStream is=conn.getInputStream();
return is;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
/*
*用W3C DOM对返回的XML进行解释
*
*/
public static String getWeather(String city)
{
try
{
Document doc;
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db=dbf.newDocumentBuilder();
InputStream is=getSoapInputStream(city);
doc=db.parse(is);
NodeList nl=doc.getElementsByTagName("getWeatherResult");
Node n=nl.item(0);
String weather=n.getFirstChild().getNodeValue();
is.close();
return weather;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
}
|
|
写个main方法检验一下结果
以下内容为程序代码:
public static void main(String[] args)throws Exception
{
System.out.println(WeatherReport.getWeather("珠海"));
}
|
|
结果如下
以下内容为程序代码:
城市==珠海,日期==4.02-4.03,图1==http://weather.tq121.com.cn/images/a1.gif,图2==http://weather.tq121.com.cn/images/00.gif,天气==多云,温度==28℃~22℃,风==微风,紫外线==弱
|
|