Http方式发送Soap报文调用WebService
WebService的实现方式之一就是基于HTTP发送SOAP报文进行调用。
可能由于各种原因,我们不能使用CXF、AXIS等框架调用,此时的解决方案之一就是直接基于HTTP发送SOAP报文,然后将响应报文当作XML进行处理。
废话不多,直接上代码:
需要导入类:
1 import org.apache.commons.httpclient.HttpClient;
2 import org.apache.commons.httpclient.UsernamePasswordCredentials;
3 import org.apache.commons.httpclient.auth.AuthScope;
4 import org.apache.commons.httpclient.methods.PostMethod;
5 import org.apache.commons.httpclient.methods.RequestEntity;
6 import org.apache.commons.httpclient.methods.StringRequestEntity;
7 import org.apache.commons.httpclient.params.HttpClientParams;
核心代码:
private String invokeService(String operationName, BaseRequestModel requestModel) throws Exception {
String url = endpointUrl;
String userName = "beiifeng";
String password = "beiifeng";
int timeOut = 60;
String targetNameSpace = "http://service.ws.beiifeng.com";
// 创建Http客户端
HttpClient httpclient = new HttpClient();
// 权限用户验证
httpclient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
PostMethod post = null;
try {
post = new PostMethod(url);
HttpClientParams params = new HttpClientParams();
params.setSoTimeout(timeOut * 1000);
httpclient.setParams(params);
RequestEntity entity = new StringRequestEntity(this.formatSOAPRequest(operationName, targetNameSpace, requestModel), "text/xml", "UTF-8");
post.setRequestEntity(entity);
int result = httpclient.executeMethod(post);
//异常
String res = post.getResponseBodyAsString();
if (result != 200) {
throw new RuntimeException("调用webservice失败:服务器端返回HTTP code " + result + "\n信息:" + res);
}
return res;
} catch (Exception e) {
throw e;
} finally {
if (post != null) {
post.releaseConnection();
}
}
}
private String formatSOAPRequest(String operationName, String targetNameSpace, BaseRequestModel requestModel) {
StringBuffer sb = new StringBuffer();
sb.append("<SOAP-ENV:Envelope xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" " +
"xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://" +
"www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instanc" +
"e\">\n");
sb.append("<SOAP-ENV:Body>\n");
sb.append("<m:" + operationName + " xmlns:m=\"" + targetNameSpace + "\">\n");
sb.append(requestModel.requestModelToString());
sb.append("</m:" + operationName + ">\n");
sb.append("</SOAP-ENV:Body>\n");
sb.append("</SOAP-ENV:Envelope>\n");
return sb.toString();
}
请求实体类继承自BaseRequestModel,代码如下:
public abstract class BaseRequestModel {
/**
* 将请求实体转换为类似XML字符串
*
* @return soapAction中的XML格式字符串
*/
public abstract String requestModelToString();
}
总结:其中滋味,各位看客自行体会:p
<<endl
Http方式发送Soap报文调用WebService的更多相关文章
- java使用POST发送soap报文请求webservice返回500错误解析
本文使用JAX-WS2.2编译webservice,并使用HttpUrlConnection的POST方式对wsdl发送soap报文进行请求返回数据, 对错误Server returned HTTP ...
- python通过http请求发送soap报文进行webservice接口调用
最近学习Python调用webservice 接口,开始的时候主要采用suds 的方式生产client调用,后来发现公司的短信接口采用的是soap报文来调用的,然后开始了谷歌,最后采用httplib ...
- Java 发送SOAP请求调用WebService,解析SOAP报文
https://blog.csdn.net/Peng_Hong_fu/article/details/80113196 记录测试代码 SoapUI调用路径 http://localhost:8082/ ...
- HttpURLConnection 直接发送soap消息调用webservice
httpConn = (HttpURLConnection) new URL(urlString).openConnection(); httpConn.setRequestProperty(& ...
- C#如何使用Soap协议调用WebService?
WebService是什么?它的作用? WebService是一个平台独立.低耦合的.自包含的.基于可编程的可使用xml描述.调用的web应用程序,用于开发分布式的交互式的应用程序. Soap是什么? ...
- Jmeter发送SOAP请求对WebService接口测试
Jmeter发送SOAP请求对WebService接口测试 1.测试计划中添加一个用户自定义变量 2.HTTP信息头管理器,添加Content-Tpe, application/soap+xml;c ...
- Java发布webservice应用并发送SOAP请求调用
webservice框架有很多,比如axis.axis2.cxf.xFire等等,做服务端和做客户端都可行,个人感觉使用这些框架的好处是减少了对于接口信息的解析,最主要的是减少了对于传递于网络中XML ...
- Java 用HTTP的方式发送JSON报文请求
前言: 项目调用第三方接口时,通常是用socket或者http的通讯方式发送请求:http 为短连接,客户端发送请求都需要服务器端回送响应,请求结束后,主动释放链接.Socket为长连接:通常情况下S ...
- Java调用WebService方法总结(8)--soap.jar调用WebService
Apache的soap.jar是一种历史很久远的WebService技术,大概是2001年左右的技术,所需soap.jar可以在http://archive.apache.org/dist/ws/so ...
- 发送Http请求调用webService
如果调用WebService的不是在.NET中,无法直接添加web引用,那怎么调用webservice. 有两种方式 第一种方式:GET方式 string strUrl = "http:// ...
随机推荐
- web基础(4): CSS布局与定位
chapter 5 CSS 布局与定位 web 前端开发的时候不是马上就考虑字体 字号这些细节,而是要先定下布局,也就是页面结构. 右图中一个个栏目就像是"盒子",每个盒子的 ...
- 激光雷达ldlidar communication is abnormal.
查看串口设置是否正常,可以通过cat /dev/ttyS4查看是否有数据以确定所接的串口是否正确
- 初学银河麒麟linux笔记 第四章 windows中开发的QT程序适配linux的修改——error: ‘QT_WARNING_DISABLE_DEPRECATED’ does not name a type
QT程序本身在windows中进行开发的,移植到linux系统上进行编译后发现了不少问题,需要一一进行修改 1.系统时间修改 首先是系统时间问题 SYSTEMTIME current_date_tim ...
- 【闲话】Vscode+PlatformIO+esp-idf+esp32物联网开发小记之环境搭建
Vscode作为一款优秀的代码编辑器,具有极为方便快捷的代码辅助与拓展功能,使用熟练后开发效率大大提高,且作为典型的IDE,不需要花费大量的时间成本即可上手,Vscode结合各种插件,可以搭建出大部分 ...
- mysql中char和varchar的区别
char的长度是不可变的,是定长的, varchar的长度是可变的,不定长的: 但是char的存取速度比varchar快,因为其长度固定,方便存储和查找. char空间换时间,varchar时间换空间 ...
- 微信小程序图片和签名
图片上传功能 chooseImage(e) { wx.chooseImage({ sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片 source ...
- 小白之Python-基础中的基础05
Python-基础中的基础05 --之元组 #元组:不可变更1. 创建元组:元组必须用,表示,只加()是不行的 print 42 print (42) print 43, #这个地方会把下一行的内容打 ...
- You are using pip version 8.1.2, however version 23.0 is available.You should consider upgrading via the 'pip install --upgrade pip' command.
今天使用python2安装es模块时报错: 原因是pip(模块管理工具)版本过低,需先升级pip,再进行安装 先替换pip的镜像,默认镜像拉取慢,还可能会失败 cd ~;mkdir .pip;touc ...
- springboot整合mybatis以及mybatis源码分析
1.@ComponentScan basePackages与value: 用于指定包的路径,进行扫描 basePackageClasses: 用于指定某个类的包的路径进行扫描 nameGenerato ...
- urllib编码问题踩坑
今天在使用python的urllib对网页进行爬取的时候,中文一直出现错误 UnicodeEncodeError: 'ascii' codec can't encode character '\u62 ...