在实际开发中,会遇到各种各样的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. L140

    一本载有许多时装照片的杂志是用带有光泽的优质纸印制的.A glossy magazine has lots of pictures of fashionable clothes and is prin ...

  2. SWF文件加密、混淆

    简单说下SWF文件的混淆原理,(已经明白的请跳过本段):我们的AS源代码被编译完成后,SWF内部会形成一个字符串映射表,包含源码里出现的所有字符串(类名,包名,成员变量名,常量等).一个数字(相当于地 ...

  3. margin特性深入分析与总结

    今天写了个小demo,总体不难,但一些细节需要注意: 1)如下图所示,蓝色区域为白色box的padding,橙色区域为每条数据项的margin-bottom,那么如何解决最后一条记录margin-bo ...

  4. 《Ubuntu入门基础》第三篇

    创建虚拟磁盘

  5. (一)canvas简介

    <canvas>元素主要用来图形的绘制,通过脚本来完成(通常时js来实现): 可以利用其实现图表,游戏等项目的开发. getContext 获取画布的摸板是2d还是3d strokeRec ...

  6. HDU - 4333 :Revolving Digits (扩展KMP经典题,问旋转后有多少个不同的数字小于它本身,等于它本身,大于它本身。)

    One day Silence is interested in revolving the digits of a positive integer. In the revolving operat ...

  7. hibernate的级联(hibernate注解的CascadeType属性)

    [自己项目遇到的问题]: 新增  删除都可以实现 ,就是修改的时候无法同步更新设计三个类:  问题类scask  正文内容类text类    查看数+回复数+讨论数的runinfo类 [正文类和查看数 ...

  8. PS常用美化处理方法大全

    学习PS的同学都知道,我们日常生活中使用PS就是进行一些简单的图像美白,图像颜色的优化,其他的基本不用,在长时间的PS使用过程中本人总结了一些处理皮肤的方法,都是一些非常简单的方法,希望能够帮助那些刚 ...

  9. VSCode高效开发插件

    VSCode 必装的 10 个高效开发插件 https://www.cnblogs.com/parry/p/vscode_top_ten_plugins.html 本文介绍了目前前端开发最受欢迎的开发 ...

  10. 转载 fpga中 restoring 和 non-restoring 除法实现。

    对于non-restoring方法,主要是用rem和den移位数据比较,rem_d长度为den+nom的总长,den_d长度为den+nom的总长度,rem_d的初始值为{{d_width{1'b0} ...