由于业务需要,最近在学习web Service 的相关知识;以下是实现web Service获取数据的两种方法。
http://www.webxml.com.cn/ 该网站提供了若干好用的web服务供你参考和使用。以下的两个方法都是参考的这个网站提供的服务。
前提:下载axis相关的架包,插件,选择在eclipse中进行开发
第一个实例用asis2创建获取webService服务:
比如我们想要获取QQ的在线状态,首先在上述提供的网站中找到提供qq在线状态查询的服务。可以在服务支持中找到。
我们可以看到该服务提供的,endpoint,disco和wsdl。
第一步:创建java 工程GetQQState
第二步:右击GetQQStatejava工程,new->other->选择axis2 wizards->axis2 code generator
第三步:点击next,选择第一项
第四步:点击next,输入wsdl file location。
该地址可以在上述网站中的qq 在线状态 web服务中看到。
如
第五步:点击next,再点击next,输入output path,点击finish。成功。。。
第六步:也许你会发现成功生成的文件有错误,那么可能是你没有加入axis2所对应的jar包。建议下载axis2-1.6.2架包。
生成之后的文件结构如下图所示:
测试代码:
package test;
import java.rmi.RemoteException;
import cn.com.webxml.QqCheckOnline;
import cn.com.webxml.QqOnlineWebServiceStub;
public class test {
public static void main(String[] args) throws RemoteException {
QqOnlineWebServiceStub service = new QqOnlineWebServiceStub();
QqCheckOnline qqCheckOn = new QqCheckOnline();
qqCheckOn.setQqCode("12132421");
String flag = service.qqCheckOnline(qqCheckOn).getQqCheckOnlineResult();
System.out.println(flag);
}
}
第二个例子:通过获取soap请求进行编码
package com.arj.comm.util;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class WeatherReport {
/**
* 获取SOAP的请求头,并替换其中的标志符号为用户输入的城市
*
* 编写者:王景辉
*
* @param city
* 用户输入的城市名称
* @return 客户将要发送给服务器的SOAP请求
*/
private static String getSoapRequest(String city) {
StringBuilder sb = new StringBuilder();
// soap的请求可以到响应的wsdl文件中找到
sb.append("<?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> <getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">"
+ "<theCityName>" + city
+ "</theCityName> </getWeatherbyCityName>"
+ "</soap:Body></soap:Envelope>");
return sb.toString();
}
/**
* 用户把SOAP请求发送给服务器端,并返回服务器点返回的输入流
*
* 编写者:王景辉
*
* @param city
* 用户输入的城市名称
* @return 服务器端返回的输入流,供客户端读取
* @throws Exception
*/
private static InputStream getSoapInputStream(String city) throws Exception {
try {
String soap = getSoapRequest(city);
if (soap == null) {
return null;
}
// url对应的是网站提供服务的endpoint
URL url = new URL(
"http://www.webxml.com.cn/WebServices/WeatherWebService.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");
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;
}
}
/**
* 对服务器端返回的XML进行解析
*
* 编写者:王景辉
*
* @param city
* 用户输入的城市名称
* @return 字符串 用,分割
*/
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("string");
StringBuffer sb = new StringBuffer();
for (int count = 0; count < nl.getLength(); count++) {
Node n = nl.item(count);
if(n.getFirstChild().getNodeValue().equals("查询结果为空!")) {
sb = new StringBuffer("#") ;
break ;
}
sb.append(n.getFirstChild().getNodeValue() + "#\n");
}
is.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 测试用
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.out.println(getWeather("长沙"));
}
}