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:// ...
随机推荐
- 配置python库源为清华源
目录 Windows Ubuntu pip较低版本 pip较高版本 Windows %HOMEPATH%/pip/pip.ini [global] index-url = https://pypi.t ...
- python_类 对象 属性
1, 类 (class) 类的概念表示某种对象的集合,用于表示某一种相同对象的模板.例如:人作为一个类 由这个"人"类定义出来的内容就是这个类定义出来的对象,类还拥有属性和功能,属 ...
- Java_类与对象
类与对象 概念 类是抽象的,概念的,代表一类事物,比如人类.猫类--,即它是数据类型. 对象是具体的,实际的,代表一个具体事物,即使实例. 类是对象的模板,对象是类的一个个体,对应一个实例 对象在内存 ...
- KingbaseES V8R3集群运维案例之---kingbase_monitor.sh启动”two master“案例
案例说明: KingbaseES V8R3集群,执行kingbase_monitor.sh启动集群,出现"two master"节点的故障,启动集群失败:通过手工sys_ctl启动 ...
- js获取对象数组中指定属性值的新数据
例: let arr = [ {name: "name1", age: "1",type:"1"}, {name: "name2& ...
- 【java数据结构与算法】直接戳中"希尔排序"要点,学不会你打我~
希尔排序: 也叫增量递减排序 是插入排序的优化,分组+排序 Get到重点,那么我们开剖析一下 结合图表和代码,总结如下~public class ShellSort { public static v ...
- JSON操作方法
一 JSON.stringify JSON.stringify() 方法是将一个JavaScript值(对象或者数组)转换为一个 JSON字符串,如果指定了replacer是一个函数,则可以替换值,或 ...
- 230219 Business 31-48
31. 31: Maternity LeaveVeronica, when is your baby due?Next month.Are you going on maternity leave? ...
- 极米投影仪安装apk的方法
https://www.touying.com/t-37871-1.html 方法二:使用U盘安装:1.使用电脑下载软件apk,并将软件apk的后缀修改为"apk1": 2.然后将 ...
- Excel error - the macros in this project are disabled, please refer to the online help or documentation of the host application to determine how to enable macros.
alt+F11 进入vba界面,F5运行macro后报错. Sub 合并当前工作簿下的所有工作表() Application.ScreenUpdating = False For j = 1 To S ...