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数据)来访问 ...
随机推荐
- ural 1146. Maximum Sum
1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...
- 一些有用的HTML5 pattern属性
最近在做手机页面时,遇到数字输入的键盘的问题,之前的做法只是一刀切的使用 type="tel",不过一直觉得九宫格的电话号码键盘上的英文字母太碍事了.于是想要尝试其它的实现方案,最 ...
- ACM 独木舟上的旅行
独木舟上的旅行 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人,且乘客 ...
- golang 自定义封包协议(转的)
package protocol import ( "bytes" "encoding/binary" ) const ( ConstHeader = &quo ...
- nodejs入门(一)
1.nodejs简介 Nodejs不是一个js应用.而是一个js运行平台. Node.js 使用事件驱动, 非阻塞I/O 模型而得以轻量和高效 2. 1).Nodejs内置了一个HTTP模块 var ...
- 让你的APP支持iPhone5
让你的APP支持iPhone5 前言 国庆节前,为了支持 iPhone5 的屏幕分辨率 (640 象素 x 1136 象素),我尝试着升级粉笔网 iPhone 客户端.整个过程花了大概一天的时间,我把 ...
- 当target属性在XHTML script中无效时
<a href="#" target=_blank></a>target此属性能够使链接在新窗口打开,但是在XHTML script中无效时. 那么解决方案 ...
- spring mvc 拦截器 拦截子目录
项目中碰到这一个问题: 对于/user/loginpage,/user/login这一类的url,放行: 对于/user/{userId}/xxx(xxx不为空)的操作,需要拦截,url-patter ...
- 单例 (JAVA)
java中单例模式是一种常见的设计模式,以下是它的特点: 单例类只能有一个实例. 单例类必须自己创建自己的唯一实例. 单例类必须给所有其他对象提供这一实例 第一种(懒汉,线程不安全): 1 publ ...
- Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds 解决方法
Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds. If the server requires ...