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:// ...
随机推荐
- element中 popconfirm与tooltip同时使用
el-popconfirm与el-tooltip同时使用 代码 <el-popconfirm confirmButtonText="确定" cancelButtonText= ...
- Mysql压缩包版本安装
之前一直使用的是安装包,今天懒得去下载了,直接用压缩包安装,记录一下. 1.先去官网下载压缩包.https://dev.mysql.com/downloads/mysql/8.0.html 2.将文件 ...
- Burp学院-信息泄露
1.Information disclosure in error messages错误消息中的信息泄露 GET /product?productId=1 发送到Repeater. 修改product ...
- 用keil调试程序的时候,一点击调试就弹出STARTUP.A51那个窗口,解决办法
前天晚上我折腾了很久 网上查了各种方法.最终自己发现,调试之前一定要在keil编译一遍,再debug这样就不会弹窗了. 另外,keil在调试过程中,修改代码是不会有任何作用的,你看我故意写错,继续单步 ...
- [前端js] 爬取亿图脑图大纲
这段程序使看到了好的东西,又没有零钱的产物 还是老师让画思维导图我不想画还想白嫖的想法 用时20分钟 就拿这个来作为例子 https://mm.edrawsoft.cn/template/286842 ...
- 【azw3】麻省理工深度思考法:从模型及动力机制来思考现象
书本详情 标题:麻省理工深度思考法:从模型及动力机制来思考现象作者:[日]平井孝志 著:张玉虹 译年份:2018出版社:北京:中国华侨出版社ISBN:9787511373441,7511373445格 ...
- Linux学习 --- 系统网络配置
前言 : 如果一台计算机想接入互联网,必须配置好IP地址,子网掩码,网关,DNS服务器.在Linux系统中,这些信息都可以修改对应的配置文件来进行配置.临时配置一下网络可以使用一些简单的命令来进行配置 ...
- 软件工程日报六——TextView和button
今天继续学习安卓stduio的知识--TextView和button TextView是安卓stduio中十分重要的一个控件,它可以在安卓应用上显示文字 通过网络我找到了TextView的相关用法如下 ...
- ORCAD中,怎么一次性去掉所有元器件下面的下划线呢
选择元器件,右键找到unset单击就可以去掉了
- Unity 在2D中实现LookAt,跟随鼠标转动
Vector3 v = (target.position - transform.position).normalized; transform.right = v;