webService 发送soap请求,并解析返回的soap报文
本例应用场景:要做一个webService测试功能,不局限于任何一种固定格式的webService,所以像axis,cxf等框架就不好用了。只有深入到webService的原理,通过发收soap报文,来调用服务返回结果。
发送请求:
/**
* 通过httpClient发送soap报文
* @param requestSoap 请求报文
* @param serviceAddress 请求地址
* @param charSet 字符集
* @param contentType 返回的contentType
* @return 响应报文
* @throws WebServiceModuleRuntimeException
*/
public String sendRequestSoap(String requestSoap, String serviceAddress, String charSet, String contentType)
throws WebServiceModuleRuntimeException {
String resultSoap = "";
PostMethod postMethod = new PostMethod(serviceAddress);
byte[] b = new byte[0];
try {
b = requestSoap.getBytes(charSet);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
InputStream is = new ByteArrayInputStream(b, 0, b.length);
RequestEntity re = new InputStreamRequestEntity(is, b.length, contentType);
postMethod.setRequestEntity(re); HttpClient httpClient = new HttpClient();
int statusCode = 0;
try {
statusCode = httpClient.executeMethod(postMethod);
System.out.println("statusCode = " + statusCode);
} catch (IOException e) {
throw new WebServiceModuleRuntimeException("执行http请求失败", e);
}
if (statusCode == 200) {
try {
resultSoap = postMethod.getResponseBodyAsString();
} catch (IOException e) {
throw new WebServiceModuleRuntimeException("获取请求返回报文失败", e);
}
} else {
throw new WebServiceModuleRuntimeException("请求失败:" + statusCode);
} return resultSoap;
}
//调用请求方法,发送报文
String responseSoap = "";
try{
responseSoap = webServiceService.sendRequestSoap(requestSoap,struct.getWebAddress(),"utf-8","text/xml; charset=utf-8");
}catch (WebServiceModuleRuntimeException ex){
throw new ModuleException("发动请求失败",ex);
}
解析返回报文:
因没有固定格式,所以无法通过jaxb工具来xml转bean,更没有客户端代码可以用。所以只有解析返回报文中,可以标识返回结果的值,比如成功、success、ok等。
此处考虑两种情况:第一种状态码放在标签的属性值中,第二种状态作为标签的内容:
<result ResultCode="0" ResultCodeDesc="成功">
<result_code>0</result_code>
System.out.println(parseResponseSoap("result_code", "", responseSoap)); /**
* 解析返回报文
* @param node 标记所在节点
* @param attr 标记所在属性
* @param soap 报文
* @return 标记值
* @throws WebServiceModuleRuntimeException
*/
public static String parseResponseSoap(String node, String attr, String soap) throws WebServiceModuleRuntimeException {
//然后用SOAPMessage 和 SOAPBody
Document personDoc;
try {
personDoc = new SAXReader().read(new StringReader(soap));
Element rootElt = personDoc.getRootElement(); // 获取根节点
Iterator body = rootElt.elementIterator("Body");
while (body.hasNext()) {
Element recordEless = (Element) body.next();
return nextSubElement(node,attr,recordEless);
}
} catch (DocumentException e) {
throw new WebServiceModuleRuntimeException("解析返回报文失败", e);
}
return "";
}
/**
* 递归方法,查找本节点是否有标记信息,如果没有就查找下一层,
* 在下一层里同样查找本层节点,只要找到值,就层层返回。
* @param node 节点标签名
* @param attr 节点属性值
* @param el 当前节点对象
* @return 目标值
*/
public static String nextSubElement(String node, String attr, Element el) {
if (el.getName().equals(node)) {
//说明 找到了目标节点
//属性值为空说明取标签内容
if (attr.equals("")) {
Iterator sub2 = el.elementIterator();
//有子节点说明标签内容不是单一值,需要拿到查询结果
if (sub2.hasNext()) {
while (sub2.hasNext()) {
Element s2 = (Element) sub2.next();
//如果返回的不是单一的标记值,而是查询结果,有些麻烦,
//查询结果应当是list<map>格式,但是map的key值不好确定,是标签名作为key还是属性值作为key
//todo
}
} else {
return el.getText();
} } else {
Attribute attrbute = el.attribute(attr);
return attrbute.getText();
}
} else {
Iterator sub2 = el.elementIterator();
while (sub2.hasNext()) {
Element sub = (Element) sub2.next();
return nextSubElement(node, attr, sub);
}
}
return "";
}
后记:本篇代码满足我自己的需求,但是看官的需求各异,本篇仅提供部分参考。
webService 发送soap请求,并解析返回的soap报文的更多相关文章
- 使用HttpClient来异步发送POST请求并解析GZIP回应
.NET 4.5(C#): 使用HttpClient来异步发送POST请求并解析GZIP回应 在新的C# 5.0和.NET 4.5环境下,微软为C#加入了async/await,同时还加入新的Syst ...
- 通过java代码HttpRequestUtil(服务器端)发送HTTP请求并解析
关键代码:String jsonStr = HttpRequestUtil.sendGet(config.getAddress() + config.getPorts() + config.getFi ...
- java内部发送http请求并取得返回结果,修改response的cookie
public Object userLogin(HttpServletRequest request, HttpServletResponse response, String email, Stri ...
- JMeter发送get请求并分析返回结果
在实际工作的过程中,我们通常需要模拟接口,来进行接口测试,我们可以通过JMeter.postman等多种工具来进行接口测试,但是工具的如何使用对于我们来说并不是最重要的部分,最重要的是设计接口测试用例 ...
- 从客户发送http请求到服务器返回http之间发生了什么
由于我知识有限,可能会有模糊或者错误的地方,欢迎讨论与指正. 1.浏览器发出http请求 当用户访问一个url时,浏览器便会开始生成一个http请求. 首先获取http请求中所需要的参数,如url,c ...
- autojs,autojs 发送http请求,autojs 解析json数据
如题,我这个就直接上代码吧 (function () { let request = http.request; // 覆盖http关键函数request,其他http返回最终会调用这个函数 http ...
- WebService如何封装XML请求 以及解析接口返回的XML
原 WebService如何封装XML请求 以及解析接口返回的XML 置顶 2019年08月16日 15:00:47 童子泛舟 阅读数 28 标签: XML解析WebService第三方API 更多 ...
- [转]C#通过Http发送Soap请求
/// <summary> /// 发送SOAP请求,并返回响应xml /// </summary> /// <param na ...
- Android使用webService(发送xml数据的方式,不使用jar包)
Android使用webService可以用ksoap2.jar包来使用.但是我觉得代码不好理解,而且记不住. 所以我查询了好多资料,以及自己的理解.可以用代码发送http请求(发送xml数据)来访问 ...
随机推荐
- ORACLE使用EXPDP和IMPDP数据泵进行导出导入的方法
ORACLE使用EXPDP和IMPDP数据泵进行导出导入的方法 (2010-05-28 12:54:34) http://blog.sina.com.cn/s/blog_67d41beb0100ixn ...
- eclipse 高亮配对的括号
在编辑代码框右键->preference,直接就可以看到Matching brackets highlights
- js+css立体旋转
纯 CSS3 制作可口可乐罐 这个效果相信大家很多人看过了,纯css实现的立体可口可乐罐,看起来相当高大上~ 于是今天我这小菜鸟试着研究下,稍微遗憾的是,没有看到源码,还是直接F12吧,貌似实现也不 ...
- 【POJ】3974 Palindrome
http://poj.org/problem?id=3974 题意:求s的最长回文串.(|s|<=1000000) #include <cstdio> #include <cs ...
- 【HDU】4418 Time travel
http://acm.hdu.edu.cn/showproblem.php?pid=4418 题意:一个0-n-1的坐标轴,给出起点X.终点Y,和初始方向D(0表示从左向右.1表示从右向左,-1表示起 ...
- 【HDU】4405 Aeroplane chess
http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意:每次可以走1~6格,初始化在第0格,走到>=n的格子就结束.还有m个传送门,表示可以从X[i] ...
- BestCoder Round #77
T1 xiaoxin juju needs help 计算组合数然后多重集排列乱搞,注意判无解情况(TM我就判错然后FST了). #include<cstdio> #include< ...
- 用css3实现一个带缺口的圆圈(图)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java实现单链表反转
一.简介 经查阅,主要有两种方法实现链表反转,递归反转法和遍历反转法: 递归: 在反转当前结点之前先反转其后边的结点,即.从尾结点开始逆向反转各个节点的指针域指向: 遍历:从前往后反转各个结点的指针域 ...
- Lamp下安全配置随笔
Apache方面: 1.apache有两个指令可以输出服务器的细节,即ServerSignature和ServerTokens. 当这两个指令一起使用时,会输出apache的版本号,php的版本号,i ...