在实际开发中,会遇到各种各样的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的更多相关文章

  1. jquery+ajax跨域请求webservice

    最近几天在学习webservice...在学习的时候便想到用ajax的方式去请求webservice.. 一直在测试..如果这个被请求的webservice和自己使用的是同一个端口号.则不用考虑那aj ...

  2. Node.js 使用 soap 模块请求 WebService 服务接口

    项目开发中需要请求webservice服务,前端主要使用node.js 作为运行环境,因此可以使用soap进行请求. 使用SOAP请求webservice服务的流程如下: 1.进入项目目录,安装 so ...

  3. JQuery请求WebService返回数据的几种处理方式

    打开自己的博客仔细浏览了一番,发现已经好久没有写博客了,由于最近一直比较忙碌懈怠了好多.默默反省三分钟.......言归正传,现在就对最近在学习webservice的过程中遇到的几种类型的问题中我的理 ...

  4. ajax请求webservice时抛出终止线程的异常

    请求webservice中以下接口,会抛出异常 {"Message":"正在中止线程.","StackTrace":" 在 Sys ...

  5. webserive学习记录6-页面请求webservice

    前面都是通过JAVA代码访问webservice服务,下面将介绍通过javascript,jquery访问webservice服务并介绍过过servlet解决跨域问题的方法. 服务端 编写服务代码,解 ...

  6. 通过HttpClient请求webService

    通过HttpClient请求webService 由于服务端是用webService开发的,android要调用webService服务获取数据,这里采用的是通过HttpClient发送post请求, ...

  7. Kettle通过Http post请求webservice接口以及结果解析处理

    kettle中有两种方式请求webservice服务,一个是Web服务查询,但是这个有缺陷,无法处理复杂的需求,遇到这种情况就需要用Http post来处理了. 网上也有很多关于Http post请求 ...

  8. ajax请求webservice的过程中遇到的问题总结

    前台用ajax的post方法,无法请求到webservice中的方法的时候,需要在配置文件中添加 web.config文件中的 <system.web> 节点下加入:<webServ ...

  9. AJAX请求WebService

    1.WebService代码 [WebMethod] [ScriptMethod(UseHttpGet = false)] public string GetObject() { User user ...

  10. .net请求Webservice简单实现天气预报功能

    很久没有接触Webservice的知识,今天稍微复习了一下关于webservice,简单做了一个天气预报的功能,虽然界面丑的厉害,但功能算是实现了,以下是效果展示. 这东西没什么难点,只是天气预报的功 ...

随机推荐

  1. springmvc 请求和响应的json和Object的转换

    就是两个注解的使用@RequestBody和@ResponseBody注解的使用,然后springmvc解析进行转换然后注入 例子: @RequestMapping("/...") ...

  2. WEB-INFO里面的jsp文件不能通过href 访问,而只能通过 servlet访问

    href="<%=basePath %>index.jsp"> 这种确实 可以在jsp页面跳转至另一个页面,只不过要放在WEB-INFO外面,也就是项目根目录下面 ...

  3. 2017.11.17 Demo-stm8+temperature timeing control

    1Find the lab and add in project. Downtown  it from ST  official website..compile it to ensure it pa ...

  4. 毕业生、程序猿转岗该如何选择Java、大数据和VR?

    许久不见的朋友请我吃饭,期间给我介绍他一个弟弟,说明年要毕业了,还不知道找啥工作,说有培训机构让他学VR.大数据什么的,不知道前景咋样,想咨询一下我.相信很多朋友面临毕业,都不知道该从事哪个行业,自己 ...

  5. Weex入门篇——Mac 安装Weex

    相关文档:http://blog.csdn.net/jasonblog/article/details/51863173 前言 相比较于React Native的“Learn once, write ...

  6. JAMstack 技术要点

    1.  简要说明 Modern web development architecture based on client-side JavaScript, reusable APIs,and preb ...

  7. 笔记:LIR2032 电池充电记录

    笔记:LIR2032 电池充电记录 LIR2032 电池是锂电池,形状和 CR2032 一样,只不过可以充电,材料是锂离子. 一个单颗的 LIR2032 电池容量只有 40mAH,容量很小. 那么就需 ...

  8. 升级3.4成3.6 ubuntu14.04 和miniconda虚拟环境

    打开Anaconda Prompt窗口conda update conda 先升级conda激活要升级python的虚拟环境conda install python=3.6.6 再升级pythonco ...

  9. Memcached的限制和使用建议

    1. 在Memcached中可以保存item数据量没有限制的,只要内存足够 2. Memcached单进程最大使用内存2G,要使用更多内存,可以分多个端口开启多个Memcached进程 3. Memc ...

  10. 多线程设计模式(二):Future模式

    一.什么是Future模型: 该模型是将异步请求和代理模式联合的模型产物.类似商品订单模型.见下图: 客户端发送一个长时间的请求,服务端不需等待该数据处理完成便立即返回一个伪造的代理数据(相当于商品订 ...