这几天在写webservice接口,其他的调用方式要生成客户端代码,比较麻烦,不够灵活,今天学习了一下httpclient调用ws的方式,感觉很实用,话不多说,上代码

http://testhcm.ycgwl.com:8070/hcm/service/MDMWebService?wsdl

package com.ycgwl.rosefinch.module.basedev.server.services.base.util;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity; import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*; public class HttpClientTest {
/**
* 访问服务
*
* @param wsdl wsdl地址
* @param ns 命名空间
* @param method 方法名
* @param list 参数
* @return
* @throws Exception
*/
public synchronized static String accessService(String wsdl, String ns, String method, List<String> list, String result) throws Exception {
StringBuffer stringBuffer = new StringBuffer(); //拼接参数
for (int i = 0; i < list.size(); i++) {
stringBuffer.append("<arg" + i + ">" + list.get(i) + "</arg" + i + ">");
} String soapResponseData = "";
//拼接SOAP
StringBuffer soapRequestData = new StringBuffer(""); // String arg0="{\"system\": \"BDMSigner\", \"keyCode\": \"SVP26FTvzwMTMWDVDP9jZw==\",\"change\": \"2018-05-12 18:17:05\"}";
// String arg1="TB_ORG_POSITION";
// String arg2="2018-05-12 18:17:05";
// String arg3="2018-05-12 18:17:05";
// String arg4="1"; soapRequestData.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.info.employee.staffing.talentbase.neusoft.com/\">");
soapRequestData.append("<soapenv:Header/>");
soapRequestData.append("<soapenv:Body>");
soapRequestData.append("<ser:" + method + ">");
soapRequestData.append(stringBuffer);
// soapRequestData.append("<arg0>"+arg0+"</arg0>");
// soapRequestData.append("<arg1>"+arg1+"</arg1>");
// soapRequestData.append("<arg2>"+arg2+"</arg2>");
// soapRequestData.append("<arg3>"+arg3+"</arg3>");
// soapRequestData.append("<arg4>"+arg4+"</arg4>");
soapRequestData.append("</ser:" + method + ">");
soapRequestData.append("</soapenv:Body>" + "</soapenv:Envelope>");
PostMethod postMethod = new PostMethod(wsdl);
// 然后把Soap请求数据添加到PostMethod中
byte[] b = null;
InputStream is = null;
try {
b = soapRequestData.toString().getBytes("utf-8");
is = new ByteArrayInputStream(b, 0, b.length);
RequestEntity re = new InputStreamRequestEntity(is, b.length, "text/xml; charset=UTF-8");
postMethod.setRequestEntity(re);
HttpClient httpClient = new HttpClient();
int status = httpClient.executeMethod(postMethod); if (status == 200) {
soapResponseData = getMesage(postMethod.getResponseBodyAsString(), result);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
is.close();
}
}
return soapResponseData;
} public static String getParam(Map<String, String> params) {
String param = "";
if (params != null) {
Iterator<String> it = params.keySet().iterator();
while (it.hasNext()) {
String str = it.next();
param += "<" + str + ">";
param += params.get(str);
param += "</" + str + ">";
}
}
return param;
} public static String getMesage(String soapAttachment, String result) {
if (result == null) {
return null;
}
if (soapAttachment != null && soapAttachment.length() > 0) {
int begin = soapAttachment.indexOf("<return>");
begin = soapAttachment.indexOf(">", begin);
int end = soapAttachment.indexOf("</return>");
String str = soapAttachment.substring(begin + 1, end);
str = str.replaceAll("<", "<");
str = str.replaceAll(">", ">");
return str;
} else {
return "";
}
} /**
* @param args
*/
public static void main(String[] args) {
try { Date useDate = new Date();
SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String beginDate = format0.format(useDate);
String endDate = format0.format(useDate);
List list = new ArrayList();
list.add(SecurityUtil.getHcmKey("BDMSigner", "yhn@BDM123", "mix@CODE"));
list.add("TB_ORG_POSITION");
list.add(beginDate);
list.add(endDate);
list.add("1");
String wsdl = "http://testhcm.ycgwl.com:8070/hcm/service/MDMWebService";
String ns = "http://service.info.employee.staffing.talentbase.neusoft.com/";
String method = "getHrInfo";
String response = accessService(wsdl, ns, method, list, "result"); } catch (Exception e) {
e.printStackTrace();
}
} }

最新文章

  1. CentOS配置本地yum源(使用镜像iso文件)
  2. spring security自定义拒绝访问页面
  3. 命令行模式下 MYSQL导入导出.sql文件的方法
  4. unity, 查看build版log文件
  5. IOS, xib和storyboard的混用--参考
  6. 关于js当中一些糟糕的特性
  7. Qt 学习之路 2(79):QML 组件
  8. Alljoyn 概述(2)
  9. SpringMVC入门2-注解开发
  10. 咸鱼入门到放弃10--javaweb的两种开发模式
  11. Linux 环境下安装Redis的步骤
  12. &lt;数据结构&gt; 队列[转]
  13. VsCode云端版本
  14. 用Java执行Python:Jython踩坑笔记
  15. Ubuntu 14.04 LTS 系统空间不足,输入密码后,无法进入桌面的解决办法
  16. 属性动画总结(Property Animation)
  17. PHP最全笔记(四)(值得收藏,不时翻看一下)
  18. 【git】之push异常
  19. 《Linux内核分析》 第五节 扒开系统调用的三层皮(下)
  20. lmathlib文件

热门文章

  1. 线段树(单点更新) HDOJ 4288 Coder
  2. 题解报告:poj 1985 Cow Marathon(求树的直径)
  3. 【转】log4j的日志
  4. log4go折腾
  5. AJPFX关于学习java遇到的问题:对算法和数据结构不熟悉
  6. Excel数据导入SQL Server
  7. 固定table表头
  8. swfit:运算符重载 Operator Methods
  9. Python3简明教程(十二)—— 模块
  10. DROP CAST - 删除一个用户定义的类型转换