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 ...
随机推荐
- Android 底部菜单会被顶起来的情况
描述:主界面有一排底部菜单,当从主界面跳转到另一个界面,假如说这个界面有软键盘弹出,主界面的顶部菜单会被顶起来. 原因:系统软键盘造成的 解决办法:在返回主界面时将系统软键盘关掉即可
- IntelliJ IDEA常用设置(一)
一.java文件中代码有错误,不点开java文件就不提示错误解决方法,版本不同可能界面有所区别. -->File->Settings->Build,Execution,Deploym ...
- 一篇文章让你彻底掌握 shell 语言
一篇文章让你彻底掌握 shell 语言 由于 bash 是 Linux 标准默认的 shell 解释器,可以说 bash 是 shell 编程的基础. 本文主要介绍 bash 的语法,对于 linux ...
- Luogu P3327 [SDOI2015]约数个数和
又是恶心的莫比乌斯反演,蒟蒻我又是一脸懵逼的被CXR dalao狂虐. 题目要求\(ans=\sum_{i=1}^n \sum_{j=1}^m d(ij)\),其中\(d(ij)\)表示数\(x\)的 ...
- 4358: permu
4358: permu 链接 分析: 不删除的莫队+可撤销的并查集. 每次询问先固定左端点到一个块内,然后将这些右端点从小到大排序,然后询问的过程中,右端点不断往右走,左端点可能会撤销,但是移动区间不 ...
- Opencv 2.4.10 +VS2010 项目配置
资料来源:http://blog.csdn.net/scottly1/article/details/40978625
- win8系统本地服务网络受限cpu占用率过高解决方案
今天更新软件时突然就打不开软件了,接着cpu就飙升. 打开任务管理器看到是“本地服务网络受限”这么一个东西占用的cpu最高. 在网上找到的解决方案无效的: 1.关闭家庭组(服务里的homegroup· ...
- js实现随机的四则运算题目(2)-更新界面
上次的代码提交完成后,有很多bug.比如函数会重复调用执行,每点击一次按钮都会在生成题目的下方直接生成新的题目,于是我在代码前面添加了如下的代码: function play_allE() { doc ...
- linux及安全第六周总结
进程控制块pcb——task_struct 操作系统三大功能: 进程管理(核心) 内存管理 文件系统 为了管理进程,内核必须对每个进程进行清晰的描述,进程描述符提供了内核所需了解的进程信息: 进程状态 ...
- “数学口袋精灵”App的第三个Sprint计划----开发日记
一.现状 上一阶段基本完成一个小游戏,游戏具有:随机产生算式,判断对错功能.通过轻快的背景音乐,音效,给玩家提供一个良好的氛围. 二.任务认领 完成界面,基本功能后的后续任务: 冯美欣:设计&qu ...