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的更多相关文章

  1. java使用POST发送soap报文请求webservice返回500错误解析

    本文使用JAX-WS2.2编译webservice,并使用HttpUrlConnection的POST方式对wsdl发送soap报文进行请求返回数据, 对错误Server returned HTTP ...

  2. python通过http请求发送soap报文进行webservice接口调用

    最近学习Python调用webservice 接口,开始的时候主要采用suds 的方式生产client调用,后来发现公司的短信接口采用的是soap报文来调用的,然后开始了谷歌,最后采用httplib ...

  3. Java 发送SOAP请求调用WebService,解析SOAP报文

    https://blog.csdn.net/Peng_Hong_fu/article/details/80113196 记录测试代码 SoapUI调用路径 http://localhost:8082/ ...

  4. HttpURLConnection 直接发送soap消息调用webservice

    httpConn = (HttpURLConnection) new URL(urlString).openConnection();    httpConn.setRequestProperty(& ...

  5. C#如何使用Soap协议调用WebService?

    WebService是什么?它的作用? WebService是一个平台独立.低耦合的.自包含的.基于可编程的可使用xml描述.调用的web应用程序,用于开发分布式的交互式的应用程序. Soap是什么? ...

  6. Jmeter发送SOAP请求对WebService接口测试

    Jmeter发送SOAP请求对WebService接口测试 1.测试计划中添加一个用户自定义变量 2.HTTP信息头管理器,添加Content-Tpe,  application/soap+xml;c ...

  7. Java发布webservice应用并发送SOAP请求调用

    webservice框架有很多,比如axis.axis2.cxf.xFire等等,做服务端和做客户端都可行,个人感觉使用这些框架的好处是减少了对于接口信息的解析,最主要的是减少了对于传递于网络中XML ...

  8. Java 用HTTP的方式发送JSON报文请求

    前言: 项目调用第三方接口时,通常是用socket或者http的通讯方式发送请求:http 为短连接,客户端发送请求都需要服务器端回送响应,请求结束后,主动释放链接.Socket为长连接:通常情况下S ...

  9. Java调用WebService方法总结(8)--soap.jar调用WebService

    Apache的soap.jar是一种历史很久远的WebService技术,大概是2001年左右的技术,所需soap.jar可以在http://archive.apache.org/dist/ws/so ...

  10. 发送Http请求调用webService

    如果调用WebService的不是在.NET中,无法直接添加web引用,那怎么调用webservice. 有两种方式 第一种方式:GET方式 string strUrl = "http:// ...

随机推荐

  1. NSAttributedString 多格式字符串

    NSString *aString = @"哈哈标题(必填)"; NSRange range = NSMakeRange(4, 4); //当然也可以查找NSRange range ...

  2. 如何查看mysql版本号

  3. 关于oracle中scott用户恢复到初始状态的步骤,和一些问题解决方法。

    一般恢复步骤: sqlplus 连接到sys用户sqlplus / as sysdba运行脚本进行初始恢复start ?/rdbms/admin/utlsampl.sql;恢复后,用户为锁定状态,需要 ...

  4. QT部署安装以及后续更新(一)

    Qt 官网有一个专门的资源下载网站,所有的开发环境和相关工具都可以从这里下载,具体地址是:http://download.qt.io/ 对目录结构的说明 目录 说明 archive 各种 Qt 开发工 ...

  5. 12组-Alpha冲刺-6/6

    12组-Alpha冲刺-6/6 一.基本情况 队名:字节不跳动 组长博客:https://www.cnblogs.com/147258369k/p/15573118.html 小组人数:10人 二.冲 ...

  6. (1019) rapidsvn 安装

    https://blog.csdn.net/mzpmzk/article/details/106332039

  7. 谷歌云|机密 GKE 节点可在计算优化的 C2D 虚拟机上使用

    机密 GKE 节点可用于计算优化的 C2D 虚拟机. 许多公司已采用 Google Kubernetes Engine (GKE) 作为其应用程序基础架构中的关键组件.在某些情况下,使用容器和 Kub ...

  8. File.Exists 判断不了虚拟路径

    https://www.shuzhiduo.com/topic/file-exists-%E5%88%A4%E6%96%AD%E4%B8%8D%E4%BA%86%E8%99%9A%E6%8B%9F%E ...

  9. jieba分词的分词模式比较

    sentence = "我来自中国人民大学" # 默认精确模式 words = jieba.cut(sentence) print("精确模式: %s" % & ...

  10. 【java】MVC模式和三层架构

    MVC是一种分层开发的模式 M:Model,业务模型,处理业务,存储数据,获取数据.JavaBean对象 V:  View , 视图,界面展示,展示数据.JSP或HTML C: Controller, ...