JAVA 如何调用网络上别人提供的WebService
以别人提供的天气预报的webService为例
服务地址是: http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx
进入http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?op=getWeather,我们可以看到请求和响应的XML文档规范
1. 请求的规范
POST /WebServices/WeatherWS.asmx HTTP/1.1
Host: webservice.webxml.com.cn
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<getWeather xmlns="http://WebXml.com.cn/">
<theCityCode>string</theCityCode>
<theUserID>string</theUserID>
</getWeather>
</soap12:Body>
</soap12:Envelope>
2. 响应的规范 (可以根据这样的一个格式来解析返回的XML文件)
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<getWeatherResponse xmlns="http://WebXml.com.cn/">
<getWeatherResult>
<string>string</string>
<string>string</string>
</getWeatherResult>
</getWeatherResponse>
</soap12:Body>
</soap12:Envelope>
下面来看下如何来发起一个请求:
(1).将请求部分中
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<getWeather xmlns="http://WebXml.com.cn/">
<theCityCode>string</theCityCode>
<theUserID>string</theUserID>
</getWeather>
</soap12:Body>
</soap12:Envelope>
另存为一个xml文件,本例中名字为weatherSoap.xml
可以将其中
...
<theCityCode>string</theCityCode>
<theUserID>string</theUserID>
...
的string替换成你自己的占位符.本例子中是替换成
...
<theCityCode>{city}</theCityCode>
<theUserID>{cityId}</theUserID>
...
(2).准备工作完毕, 下面是程序部分
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.Proxy.Type;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class GetWeather {
public static void main(String[] args) throws Exception {
}
public static String getWeather(String city, String cityId) throws Exception{
InputStream is = getSoapInputStream("南昌", ""); // 获取服务器端返回的流
BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8")); //使用UTF-8进行编码.
String temp;
while ((temp = br.readLine()) != null) {
System.out.println(temp);
}
// temp 就是返回的XML文件
/*下面可以使用DOM来解析当前返回的XML*/
// DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// DocumentBuilder db = dbf.newDocumentBuilder();
// Document doc = db.parse(is);
return "";
}
/**
* 读取请求文件
* @param city
* @param cityId
* @return
* @throws Exception
*/
public static String getResource(String city, String cityId)
throws Exception {
InputStream is = new FileInputStream(new File("weathersoap.xml"));
BufferedReader br = new BufferedReader(new InputStreamReader(is)); //创建文件输入流
StringBuffer sb = new StringBuffer();
String temp;
while ((temp = br.readLine()) != null) {
sb.append(temp); // 将文件的内容读取进来
}
br.close();
String soap = sb.toString();
soap = soap.replace("{city}", city);
soap = soap.replace("{cityId}", cityId); // 将文件中的占位符替换成我们所要传递的参数
return soap;
}
/**
* 该方法得到服务器端返回的输入流
* @param city 城市名称
* @param cityId 城市编码
* @return
* @throws Exception
*/
private static InputStream getSoapInputStream(String city, String cityId)
throws Exception {
String soap = getResource(city, cityId);
if (soap != null) {
URL url = new URL(
"http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx");
URLConnection uc = url.openConnection(new Proxy(Type.HTTP,
new InetSocketAddress("192.168.0.1", 808))); // 建立连接,该处使用代理服务器.
//如果没使用代理服务器的话, 则直接使用url.openConnection();
uc.setUseCaches(false); // 设置不缓存
uc.setDoInput(true); // 设置可以输出
uc.setDoOutput(true); // 设置可以输入
/**
* POST /WebServices/WeatherWS.asmx HTTP/1.1
* Host: webservice.webxml.com.cn
* Content-Type: application/soap+xml; charset=utf-8
* Content-Length: length
* 下面设置头文件中的信息,可以将上面请求文件中的头信息设置进去
* 其中的length = 文件内容的长度
*/
uc.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8");
uc.setRequestProperty("Content-Length", String.valueOf(soap.length()));
OutputStream os = uc.getOutputStream(); //获取连接的输入流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,
"UTF-8"));
bw.write(soap); // 将请求的内容发送到服务器
bw.flush();
bw.close();
InputStream is = uc.getInputStream(); // 获取服务器返回的流
return is;
}
return null;
}
}
(3). 下面我们就可以使用DOM解析返回的XML文件, 获取所需要的信息s
本文介绍如何使用JAVA通过SOAP协议调用远程WebService服务获取天气预报信息,包括构建请求XML、设置HTTP请求头、使用代理服务器及解析返回的XML数据。
1785

被折叠的 条评论
为什么被折叠?



