最近调用其他系统提供的webservice接口,公司的开发人员给了一份axis2框架生成的代码让我去调用。
摸索半天也没有成功调用别人的代码,倒是自己学会了如何利用HttpClient工具类发送SOAP请求调用接口
首先说说自己的理解,SOAP协议是基于HTTP协议上开发的,底层还是调用HTTP协议,或者说SOAP协议是一种特殊的HTTP协议(对HTTP协议做一些限制就形成了SOAP协议)
以别人开放的天气接口为例(http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo)
可以对比SOAP1.1和HTTP GET请求看出差异(以SOAP1.1而非SOPA1.2举例)
具体如下
1.请求路径以.asmx或.wsdl结尾(这个对应于传统Java开发中的的XXServlet)
2.多了一个SOAPAction的请求头告诉具体路径(这个对应于传统Java开发中的的XXServlet?action="XX")
3.请求内容为一段特殊的xml文件而非传统的对象字符串
4.请求方式为POST请求
返回的报文差异就在于返会一段可解析为XML的字符串
所以代码的设计思路就出来了,利用HttpClient发送请求,请求的地址,SOAPAction,XML文本内容按照提供方给的接口就行
利用DocumentHelper将返回的内容的内容解析成document对象
获取根元素,遍历其下的子元素,传入Map容器中或者bean对象中
代码如下:
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.io.IOUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.xml.sax.InputSource;
public class HosService {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String wsdl = "http://101.20.201.246:9094/ESBServicePro.asmx";
int timeout = 3000;
StringBuffer sb = new StringBuffer("");
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.append("<soapenv:Envelope "
+ " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
+ " xmlns:q0='http://WebXml.com.cn/' "
+ " xmlns:xsd='http://www.w3.org/2001/XMLSchema' "
+ " xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' >");
sb.append("<soapenv:Body>");
sb.append("<getMzUserInfo xmlns=\"http://tempuri.org/\">");
sb.append("<code>20215941</code>");
sb.append("</getMzUserInfo>");
sb.append("</soapenv:Body>");
sb.append("</soapenv:Envelope>");
// HttpClient发送SOAP请求
System.out.println("HttpClient 发送SOAP请求");
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(wsdl);
// 设置连接超时
client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
// 设置读取时间超时
client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
// 然后把Soap请求数据添加到PostMethod中
RequestEntity requestEntity = new StringRequestEntity(sb.toString(), "text/xml", "UTF-8");
//设置请求头部,否则可能会报 “no SOAPAction header” 的错误
postMethod.setRequestHeader("SOAPAction","http://tempuri.org/getMzUserInfo");
// 设置请求体
postMethod.setRequestEntity(requestEntity);
int status = client.executeMethod(postMethod);
// 打印请求状态码
System.out.println("status:" + status);
// 获取响应体输入流
InputStream is = postMethod.getResponseBodyAsStream();
// 获取请求结果字符串
String result = IOUtils.toString(is);
System.out.println(result);
Document dc = strXmlToDocument(result);
// Element root = dc.getRootElement();
// System.out.println(root.getName());
// System.out.println("result: " + result);
}
public static Document strXmlToDocument(String parseStrXml){
Document document = null;
try {
document = DocumentHelper.parseText(parseStrXml);
Element root = document.getRootElement();
List<Element> list = root.elements();
getElement(list);
} catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
private static void getElement(List<Element> sonElemetList) {
// Map<String,String> map = new HashMap<String, String>();
for (Element sonElement : sonElemetList) {
if (sonElement.elements().size() != 0) {
System.out.println(sonElement.getName() + ":");
getElement(sonElement.elements());
}else{
System.out.println(sonElement.getName() + ":"+ sonElement.getText());
}
}
}
}
参考:https://www.cnblogs.com/loong-hon/p/10337316.html