HTTP直接请求webService
在实际开发中,会遇到各种各样的webService接口,并且对方提供的接口并不规范,一些客户端反而就不好使了,如cxf(客户端与动态调用)等,
直接用java提供的api比较繁琐,这时直接用http request请求更便捷。
可以分为两步,request一个soap消息(xml),然后response一个soap消息(xml),具体如下
/**
* <br>描 述:发送并返回报文消息
* <br>作 者:xieyj
* <br>历 史: (版本) 作者 时间 注释
* @param urlStr wsdl
* @param paraXml 请求的soap消息串,可以用soapui查看
* @param intfMethod 请求的接口方法
* @return
*/
public String sendAndGetResponseData(String urlStr, String paraXml, String intfMethod) { String respData = "";
try {
URL url = new URL(urlStr); HttpURLConnection con;
con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setUseCaches(false);
//con.setRequestProperty("Content-type", "text/xml; charset=UTF-8");
//con.setRequestProperty("WSS-Password Type", "PasswordText"); con.setRequestProperty("SOAPAction", intfMethod);
con.setRequestProperty("Encoding", "UTF-8");
OutputStream reqStream = con.getOutputStream();
reqStream.write(paraXml.getBytes()); //接收报文
InputStream resStream = con.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(resStream, "utf-8")); StringBuffer data = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
data.append(line);
} //将结果解密
respData= data.toString(); } catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return respData;
}
soap请求串实例,如下
/**
* <br>描 述:请求消息模板
* <br>作 者:xieyj
* <br>历 史: (版本) 作者 时间 注释
* @return
*/
public String getXmlTemplate() { /**参数描述
* @param systemId 为数据请求系统ID
* @param dataType 传部门ID则出该部门数据,传空出全部数据
* @param bgnDt 增量数据产生开始日期时间 日期时间格式为:YYYY-MM-DD hh24:mi:ss
* @param endDt 增量数据产生结束日期时间 日期时间格式为:YYYY-MM-DD hh24:mi:ss
* @param pageNum 页码,表示请求第几页的数据
* @param pageSize 数据行数,表示请求单页传输的数据,默认为500
* @param userName 用户名,采用DES加密
* @param passWord 用户密码,采用DES加密
*/ StringBuffer template = new StringBuffer();
template.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://com.hps.webservice/admin/services/\">");
template.append("<soapenv:Header/>");
template.append("<soapenv:Body>");
template.append("<ser:${soapAction}>");
template.append("<ser:in0>");
template.append("<![CDATA[<?xml version='1.0' encoding='UTF-8'?><DATA>");
template.append("<parameter name='SYSTEM_ID'>${systemId}</parameter>");
template.append("<parameter name='DATA_TYPE'>${dataType}</parameter>");
template.append("<parameter name='BGN_DT'>${bgnDt}</parameter>");
template.append("<parameter name='END_DT'>${endDt}</parameter>");
template.append("<parameter name='PAGE_NUM'>${pageNum}</parameter>");
template.append("<parameter name='PAGE_SIZE'>${pageSize}</parameter>");
template.append("<parameter name='USERNAME'>${userName}</parameter>");
template.append("<parameter name='PASSWORD'>${passWord}</parameter>");
template.append("</DATA>]]>");
template.append("</ser:in0>");
template.append("</ser:${soapAction}>");
template.append("</soapenv:Body>");
template.append("</soapenv:Envelope>"); return template.toString();
}
占位符替换工具类见 Java占位符替换工具类
HTTP直接请求webService的更多相关文章
- jquery+ajax跨域请求webservice
最近几天在学习webservice...在学习的时候便想到用ajax的方式去请求webservice.. 一直在测试..如果这个被请求的webservice和自己使用的是同一个端口号.则不用考虑那aj ...
- Node.js 使用 soap 模块请求 WebService 服务接口
项目开发中需要请求webservice服务,前端主要使用node.js 作为运行环境,因此可以使用soap进行请求. 使用SOAP请求webservice服务的流程如下: 1.进入项目目录,安装 so ...
- JQuery请求WebService返回数据的几种处理方式
打开自己的博客仔细浏览了一番,发现已经好久没有写博客了,由于最近一直比较忙碌懈怠了好多.默默反省三分钟.......言归正传,现在就对最近在学习webservice的过程中遇到的几种类型的问题中我的理 ...
- ajax请求webservice时抛出终止线程的异常
请求webservice中以下接口,会抛出异常 {"Message":"正在中止线程.","StackTrace":" 在 Sys ...
- webserive学习记录6-页面请求webservice
前面都是通过JAVA代码访问webservice服务,下面将介绍通过javascript,jquery访问webservice服务并介绍过过servlet解决跨域问题的方法. 服务端 编写服务代码,解 ...
- 通过HttpClient请求webService
通过HttpClient请求webService 由于服务端是用webService开发的,android要调用webService服务获取数据,这里采用的是通过HttpClient发送post请求, ...
- Kettle通过Http post请求webservice接口以及结果解析处理
kettle中有两种方式请求webservice服务,一个是Web服务查询,但是这个有缺陷,无法处理复杂的需求,遇到这种情况就需要用Http post来处理了. 网上也有很多关于Http post请求 ...
- ajax请求webservice的过程中遇到的问题总结
前台用ajax的post方法,无法请求到webservice中的方法的时候,需要在配置文件中添加 web.config文件中的 <system.web> 节点下加入:<webServ ...
- AJAX请求WebService
1.WebService代码 [WebMethod] [ScriptMethod(UseHttpGet = false)] public string GetObject() { User user ...
- .net请求Webservice简单实现天气预报功能
很久没有接触Webservice的知识,今天稍微复习了一下关于webservice,简单做了一个天气预报的功能,虽然界面丑的厉害,但功能算是实现了,以下是效果展示. 这东西没什么难点,只是天气预报的功 ...
随机推荐
- 解析XML文件的几种方式及其比较
解析xml文件目前比较流行的主要有四种方式: 1. DOM(Document Object Model)它把整个XML文档当成一个对象加载到内 存,不管文档有多大.它一般处理小文件 2.SAX(Si ...
- JQuery直接调用asp.net后台WebMethod方法(转)
转自 http://blog.csdn.net/handsometone1982/article/details/7684894 利用JQuery的$.ajax()可以很方便的调用asp.net的后 ...
- H264的编解码流程?
- Leetcode 999. Available Captures for Rook
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: rook = [0, 0] ans = 0 f ...
- Java中BIO,NIO,AIO的理解
在高性能的IO体系设计中,有几个名词概念常常会使我们感到迷惑不解.具体如下: 1 什么是同步? 2 什么是异步? 3 什么是阻塞? 4 什么是非阻塞? 5 什么是同步阻塞? 6 什么是同步非阻塞? 7 ...
- R+markdown+LaTeX 中文编译解决方案
一丢丢前言 很久之前曾试图以Rmarkdown编译pdf文档,无奈怎么鼓捣都会error,搜索了很久都没能找到比较好的解决方案.在配置上将编译器调成了xeLaTeX后就不了了之.这两天心血来潮研究了一 ...
- 常用的两个PHP类
/** * Class Interval * @author logonmy * @desc 简单分析程序执行时间: */ Class Interval{ var $start; publ ...
- 帮你彻底解决eclipse(myeclipse)中写struts.xml配置文件
其实,在自己写struts.xml的时候,竟然没有代码提示功能.让我非常的烦恼,其实解决这个问题的关键还是system不知道他的dtd的规则无法提示配置信息 很简单,那就让它知道就OK了!!! 道理明 ...
- 使用DeflateStream压缩与解压
具体可以了解下:http://msdn.microsoft.com/zh-cn/library/system.io.compression.deflatestream(v=vs.110).aspx / ...
- win10笔记本用Fiddler对手机App抓包
移动客户端项目有时需要针对手机app进行抓包,这时一般有两种办法:直接下个手机抓包工具的app,在手机上抓:pc机上装上抓包工具,pc和手机连接同一个无线,在pc机上抓.第一种比较简单,但抓包工具自然 ...