一 使用 soap协议及httpclint实现从服务器请求数据,本例为号码归属地接口。
1.使用soapUI工具,得到soap报文如下:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://WebXml.com.cn/">
<soapenv:Header/>
<soapenv:Body>
<web:getRegionCountry/>
</soapenv:Body>
</soapenv:Envelope>
2.使用httpclint发出请求
/**
* 使用aoxp报文加http POST webService接口
*/
public void HttpClientLink(){
//归属地编码接口
String urlStr="http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl";
StringBuffer soapStr = new StringBuffer();
soapStr.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://WebXml.com.cn/\">");
soapStr.append("<soapenv:Header/>");
soapStr.append("<soapenv:Body>");
soapStr.append("<web:getRegionCountry/>");
soapStr.append("</soapenv:Body>");
soapStr.append("</soapenv:Envelope>");
String soapXml = soapStr.toString();
try {
// 开启HTTP连接ַ
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
// 设置HTTP请求相关信息
httpConn.setRequestProperty("Content-Length",
String.valueOf(soapXml.getBytes().length));
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
// 进行HTTP请求
OutputStream outObject = httpConn.getOutputStream();
outObject.write(soapXml.getBytes());
// 关闭输出流
outObject.close();
// 获取HTTP响应数据
InputStreamReader isr = new InputStreamReader(
httpConn.getInputStream(), "utf-8");
BufferedReader inReader = new BufferedReader(isr);
StringBuffer result = new StringBuffer();
String inputLine;
while ((inputLine = inReader.readLine()) != null) {
result.append(inputLine);
}
// 打印HTTP响应数据
//解析xml
StringReader sr = new StringReader(result.toString());
InputSource is = new InputSource(sr);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
builder = factory.newDocumentBuilder();
Document d = builder.parse(is);
NodeList nl =d.getElementsByTagName("getRegionCountryResult"); //得到xml结构
NodeList n2 = nl.item(0).getChildNodes();
System.out.println("--------------开始--------------");
for (int i=0;i<n2.getLength();i++){
System.out.println(n2.item(i).getNodeName()+"::"+n2.item(i).getTextContent());//解析元素值
}
System.out.println("--------------结束--------------");
// 关闭输入流
inReader.close();
isr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
二 使用xfire构建动态客户端
1.下载xfire jar,放入lib
2.客户端代码及返回数据解析
import java.io.IOException;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class XfrieClientAction extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* xFire访问webService
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Client client = null;
try {
client = new Client(new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl")); //归属地接口
client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "10000");
Object[] result = client.invoke("getDatabaseInfo", new Object[]{""}); //getDatabaseInfo 方法,参数为空
Document d = (Document)result[0];
NodeList nl =d.getElementsByTagName("getDatabaseInfoResult"); //使用soapUI得到相应xml报文
NodeList n2 = nl.item(0).getChildNodes();
System.out.println("--------------开始--------------");
for (int i=0;i<n2.getLength();i++){
System.out.println(n2.item(i).getNodeName()+"::"+n2.item(i).getTextContent());//解析元素值
}
System.out.println("--------------结束--------------");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
三。结语
1. 客户端构建方式多样,除了动态生成客户端,还可以使用各种wsdl2Java工具或者使用xfrie,cxf插件来生成客户端代码。
2.soapUI是很好的接口调试工具