这几天在写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();
}
} }

httpclient调用webservice接口的方法实例的更多相关文章

  1. Java之HttpClient调用WebService接口发送短信源码实战

    摘要 Java之HttpClient调用WebService接口发送短信源码实战 一:接口文档 二:WSDL 三:HttpClient方法 HttpClient方法一 HttpClient方法二 Ht ...

  2. Java 常调用的Webservice接口的方法

    WebService是基于Web的服务,WebService使用SOAP协议实现跨编程语言和跨操作系统平台,接收和响应外部系统的某种请求,从而实现远程调用.WebService采用HTTP协议传输数据 ...

  3. Java调用webservice接口方法

                             java调用webservice接口   webservice的 发布一般都是使用WSDL(web service descriptive langu ...

  4. cxf 和 httpclient 客户端调用 webservice 接口

    一.cxf 生成 webservice 客户端 1.接口路径 http://ws.webxml.com.cn/WebServices/WeatherWS.asmx 2.进入你需要放置 webservi ...

  5. php中创建和调用webservice接口示例

    php中创建和调用webservice接口示例   这篇文章主要介绍了php中创建和调用webservice接口示例,包括webservice基本知识.webservice服务端例子.webservi ...

  6. java 调用webservice的各种方法总结

    java 调用webservice的各种方法总结 几种流行的开源WebService框架Axis1,Axis2,Xfire,CXF,JWS比较 方法一:创建基于JAX-WS的webservice(包括 ...

  7. js调用Webservice接口案例

    第一步:新建Webservice接口 主文件方法 using System;using System.Collections.Generic;using System.Web;using System ...

  8. SQL调用WebService接口

    今天在做一个非常奇葩的东西.中间有个过程要在SQL触发器里面调用webservice接口.呵呵~ ALTER TRIGGER tgr_UpdateMemcached ON dbo.[User] AFT ...

  9. 动态调用WebService接口的几种方式

    一.什么是WebService? 这里就不再赘述了,想要了解的====>传送门 二.为什么要动态调用WebService接口? 一般在C#开发中调用webService服务中的接口都是通过引用过 ...

随机推荐

  1. HAL之PWM

    PWM是定时器的一个输出功能,要分配在有对应输出的管脚上.分频和定时值决定了周期,捕获寄存器的值就是占空比,当计数寄存器的值小于捕获值时输出固定电平(H),当大于时翻转电平,当计数器值溢出时将重载值载 ...

  2. STM32CUBEMX使用注意:

    一 注意堆栈大小,简单来说,栈空间用于局部变量空间(size=0x400一般够用),堆(size=0x200一般够用)空间用于 alloc 或者 malloc函数动态申请变量空间

  3. [转]Android 完美退出 App (Exit)

    本文转自:http://blog.csdn.net/zeus_9i/article/details/7259881 最近两天为了解决Android上面退出程序问题折腾了半死,在google & ...

  4. 下载JSTL方法

    第一步:访问 http://www.apache.org/dist/ 第二步:点击jakarta

  5. scroll offset & client总结

    oEvent.clientX 是指鼠标到可视区左边框的距离. oEvent.clientY 是指鼠标到可视区上边框的距离. clientWidth  是指可视区的宽度. clientHeight  是 ...

  6. win10下安装使用mysql-5.7.23-winx64

    下载MySQLhttps://dev.mysql.com/downloads/file/?id=478884 解压到文件,此例为D盘根目录 在mysql-5.7.23-winx64目录下创建[my.i ...

  7. C++构造函数(复制构造函数)、析构函数

    注:若类中没有显示的写如下函数,编译会自动生成:默认复制构造函数.默认赋值构造函数(浅拷贝).默认=运算符重载函数(浅拷贝).析构函数: 1.默认构造函数(默认值)构造函数的作用:初始化对象的数据成员 ...

  8. MySQL-06 数据备份和恢复

    学习目标 数据备份 数据恢复 数据库迁移 导入和导出 数据备份 系统意外崩溃或者服务器硬件损坏都有可能导致数据库丢失,因此生产环境中数据备份非常重要. MySQLdump命令备份 该命令可以将数据库备 ...

  9. SQL 语句解决实际问题

    在项目开发过程中,遇到数据库的查询问题 一.查询某表字段的信息 select * from syscolumns SELECT object_id('TB_KYChildProject') selec ...

  10. mysql alter修改数据库表结构用法

    1.alter操作表字段 (1)增加字段 alter table 表名 add 字段名 字段类型: alter table student add name varchar(10): (2)修改字段 ...