cxf Webservice 使用httpClient 调用
package com.wistron.wh.swpc.portal.uitl;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import com.wistron.swpc.framework.exception.SystemException;
/**
* 調用webservice的工具,封裝了post,get,put,delete方法調用webservice
*
*/
public class WebServiceUtil {
// 日志记录器
private static Logger logger = Logger.getLogger(WebServiceUtil.class);
/*
* public static String postByMap(String url, Map<String, Object> pv) throws
* SystemException{ logger.debug(String.format("Request url:%s", url));
* String responseMsg = ""; PostMethod postMethod = null; try { HttpClient
* httpClient = new HttpClient();
* httpClient.getParams().setContentCharset("utf-8"); postMethod = new
* PostMethod(url);
*
* Set<String> set = pv.keySet(); Iterator<String> it = set.iterator();
* while (it.hasNext()) { String key = it.next(); Object value =
* pv.get(key); if(null != value) postMethod.addParameter(key,
* value.toString()); else postMethod.addParameter(key, ""); }
*
* httpClient.executeMethod(postMethod);// 200 responseMsg =
* postMethod.getResponseBodyAsString().trim(); } catch(Exception e){ throw
* new SystemException("webservice请求异常",e ); } finally {
* postMethod.releaseConnection(); }
*
* return responseMsg; }
*/
/**
* post调用
*
* @param url
* @param xml
* @return
* @throws SystemException
*/
public static String postMethodInvoke(String url, String xml)
throws SystemException {
logger.debug(String.format("Request url:%s", url));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
String responseMsg = null;
try {
HttpEntity re = new StringEntity(xml, "utf-8");
httppost.setHeader("Content-Type", "application/xml;charset=utf-8");
httppost.setEntity(re);
HttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
responseMsg = EntityUtils.toString(entity, "utf-8");
} catch (Exception e) {
throw new SystemException("webservice请求异常", e);
} finally {
httpClient.getConnectionManager().shutdown();
}
return responseMsg;
}
/**
* get调用
*
* @param url
* @return
* @throws Exception
*/
public static String getMethodInvoke(String url) throws Exception {
logger.debug(String.format("Request url:%s", url));
String responseMsg = "";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
try {
httpget.setHeader("Content-Type", "application/xml; charset=utf-8");
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
responseMsg = EntityUtils.toString(entity, "utf-8");
} catch (Exception e) {
throw new SystemException("webservice请求异常", e);
} finally {
httpClient.getConnectionManager().shutdown();
}
return responseMsg;
}
/**
* put
*
* @param url
* @param xml
* @return
* @throws SystemException
*/
public static String putMethodInvoke(String url, String xml)
throws SystemException {
logger.debug(String.format("Request url:%s", url));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPut httpput = new HttpPut(url);
String state = null;
try {
HttpEntity re = new StringEntity(xml);
httpput.setHeader("Content-Type", "charset=utf-8");
httpput.setEntity(re);
HttpResponse response = httpClient.execute(httpput);
state = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
throw new SystemException("webservice请求异常", e);
} finally {
httpClient.getConnectionManager().shutdown();
}
return state;
}
/**
* delete
*
* @param url
* @return
* @throws Exception
*/
public static String deleteMethodInvoke(String url) throws Exception {
logger.debug(String.format("Request url:%s", url));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpDelete httpdelete = new HttpDelete(url);
String state = null;
try {
httpdelete.setHeader("Content-Type", "charset=utf-8");
HttpResponse response = httpClient.execute(httpdelete);
state = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
throw new SystemException("webservice请求异常", e);
} finally {
httpClient.getConnectionManager().shutdown();
}
return state;
}
/**
* post 文件下载
*
* @param url
* @param file
* @return
* @throws SystemException
*/
public static String postUploadMethodInvoke(String url, File file)
throws SystemException {
logger.debug(String.format("Request url:%s", url));
DefaultHttpClient httpClient = new DefaultHttpClient();
String state = null;
FileInputStream fis = null;
BufferedInputStream in = null;
try {
HttpPost httppost = new HttpPost(url);
fis = new FileInputStream(file);
in = new BufferedInputStream(fis);
HttpEntity re = new InputStreamEntity(in, file.length(),
ContentType.MULTIPART_FORM_DATA);
httppost.setHeader("Content-Type", "charset=utf-8");
httppost.setHeader("enctype", "multipart/form-data'");
httppost.setEntity(re);
HttpResponse response = httpClient.execute(httppost);
HttpEntity e = response.getEntity();
state = EntityUtils.toString(e == null ? new StringEntity("") : e);
} catch (Exception e) {
throw new SystemException("webservice请求异常", e);
} finally {
try {
if (in != null) {
in.close();
}
if (fis != null) {
fis.close();
}
httpClient.getConnectionManager().shutdown();
} catch (IOException e) {
throw new SystemException("webservice请求异常", e);
}
}
return state;
}
}
cxf Webservice 使用httpClient 调用的更多相关文章
- java调用CXF WebService接口的两种方式
通过http://localhost:7002/card/services/HelloWorld?wsdl访问到xml如下,说明接口写对了. 2.静态调用 // 创建WebService客户端代理工厂 ...
- java Webservice(一)HttpClient远程调用
我们将Web Service发布在Tomcat或者其他应用服务器上后,有很多方法可以调用该Web Service,常用的有两种: 1.通过浏览器HTTP调用,返回规范的XML文件内容 2.通 ...
- httpclient调用webservice接口的方法实例
这几天在写webservice接口,其他的调用方式要生成客户端代码,比较麻烦,不够灵活,今天学习了一下httpclient调用ws的方式,感觉很实用,话不多说,上代码 http://testhcm.y ...
- Java之HttpClient调用WebService接口发送短信源码实战
摘要 Java之HttpClient调用WebService接口发送短信源码实战 一:接口文档 二:WSDL 三:HttpClient方法 HttpClient方法一 HttpClient方法二 Ht ...
- ASP.NET Core教程:在ASP.NET Core中使用HttPClient调用WebService
一.前言 在以前的一篇文章中,曾经讲述过如何在ASP.NET Core中调用WebService.但是那种方式是通过静态引用的方式去调用的,如果是在生产环境中,肯定不能使用这种方式去调用,幸运的是微软 ...
- CXF WebService 教程
业务需求:常见WEB服务: 手机淘宝.京东…. 天气预报 手机号归属地 股票查询 发手机短消息 手机充值功能 中英文翻译 银行转账业务 公司的“进销存系统”在某商品缺货时自动给供应商下订单 ..... ...
- Spring整合CXF webservice restful 实例
webservice restful接口跟soap协议的接口实现大同小异,只是在提供服务的类/接口的注解上存在差异,具体看下面的代码,然后自己对比下就可以了. 用到的基础类 User.java @Xm ...
- CXF 动态创建客户端调用稳定版本号为2.7.18
今天用动态创建客户端的方式调用webservice,报了这样一个错: 2017-01-05 20:51:46,029 DEBUG main org.apache.cxf.common.logging. ...
- CXF WebService整合SpringMVC的maven项目
首先推荐博客:http://www.cnblogs.com/xdp-gacl/p/4259481.html http://blog.csdn.net/hu_shengyang/article/de ...
随机推荐
- ASP 基础三 SQL指令
一 增删改查 <% set conn=server.CreateObject("adodb.connection") DSNtemp="DRIVER={SQL Se ...
- IDEA Maven Jetty Jrebel 热部署
准备:1.下载Jrebel的离线安装包,版本是6.4.3版本. 2.下载Jrebel的破解补丁包,同样也是针对6.4.3版本的 Jrebel离线安装包官网下载地址:https:/ ...
- Linux下对inode和块的理解
基本概念 首先讲下inode和块的基本概念.在Linux系统中,文件由元数据和数据块组成.数据块就是多个连续性的扇区(sector),扇区是文件存储的最小单位(每个512字节).块(block)的大小 ...
- STM32 printf()函数和scanf()函数重定向到串口
STM32 printf()函数和scanf()函数重定向到串口 printf()函数和scanf()函数重定向 在学习STM32的时候,常常需要用串口来测试代码的正确与否,这时候就要要用到print ...
- Luogu4921/4931 情侣?给我烧了! 组合、递推
4921 4931 第一眼看着就像容斥,但是容斥不怎么好做-- 第二眼想到错排,结果错排公式糊上去错了-- 不难考虑到可以先选\(K\)对情侣坐在一起,剩下\(N-K\)对错排 选\(K\)对情侣坐在 ...
- c#对联合体的封装
https://blog.csdn.net/u012846041/article/details/37518313 标准C或者C++中均提供关键字定义联合结构,C#中未提供类似的关键字,但仍然可以定义 ...
- International Programming Retreat Day(2018.11.17)
时间:2018.11.17地点:北京国华投资大厦
- 【转】Raft 为什么是更易理解的分布式一致性算法
编者按:这是看过的Raft算法博客中比较通俗的一篇了,讲解问题的角度比较新奇,图文并茂,值得一看.原文链接:Raft 为什么是更易理解的分布式一致性算法 一致性问题可以算是分布式领域的一个圣殿级问题了 ...
- bat基础知识
1.打日志:使用重定向 eg:call test.bat>log/test.log 2.不关闭cmd窗口:使用pause eg: 结果: ps:注意,在自动化运维的时候,比如创建自动发版的脚本的 ...
- [Oracle][Metadata]如何查找与某一个功能相关的数据字典名
当Oracel的一个新功能出来的时候,我们可能不知道所有与此功能关联的数据字典名称,那么如何才能得到这些 meta data 的 meta data 呢? 可以通过 dicitonary 来查看: 例 ...