cxf Webservice 使用httpClient 调用-程序员宅基地

技术标签: java  

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;
}

}

转载于:https://www.cnblogs.com/LLLONG/p/3345241.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_30402085/article/details/95785749

智能推荐

Linux驱动开发: USB驱动开发_linux usb 通信从设备开发-程序员宅基地

文章浏览阅读7k次,点赞86次,收藏192次。一、USB简介1.1 什么是USB? USB是连接计算机系统与外部设备的一种串口总线标准,也是一种输入输出接口的技术规范,被广泛地应用于个人电脑和移动设备等信息通讯产品,USB就是简写,中文叫通用串行总线。最早出现在1995年,伴随着奔腾机发展而来。自微软在Windows 98中加入对USB接口的支持后,USB接口才推广开来,USB设备也日渐增多,如数码相机、摄像头、扫描仪、游戏杆、打印机、键盘、鼠标等等,其中应用最广的就是摄像头和U盘了。 USB包括老旧的USB 1.1标准..._linux usb 通信从设备开发

Qt知识点梳理 —— 代码实现菜单栏工具栏-程序员宅基地

文章浏览阅读379次,点赞5次,收藏9次。在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门即可获取!家准备了最新的互联网大厂资料。[外链图片转存中…(img-VMQDYeXz-1712056340129)][外链图片转存中…(img-JqBcGpUE-1712056340130)][外链图片转存中…(img-7Rrt8dF9-1712056340130)]

《设计模式入门》 9.代理模式_cglib需要引入第三方包-程序员宅基地

文章浏览阅读448次。代理模式可以说是我们在java学习中非常常见的一个设计模式了,在很多地方我们都可以看到代理模式的影子。比如:Spring 的 Proxy 模式(AOP编程 )AOP的底层机制就是动态代理 mybatis中执行sql时mybatis会为mapper接口通过jdk动态代理的方法生成接口的实现类 Feign对于加了@FeignClient 注解的类会在Feign启动时,为其创建一个本地JDK Proxy代理实例,并注册到Spring IOC容器可以看出,代理模式就是给..._cglib需要引入第三方包

前端开发:JS中向对象中添加对象的方法_一个对象如何添加另一个对象-程序员宅基地

文章浏览阅读1w次,点赞2次,收藏6次。在前端开发过程中,一切皆对象,尤其是在数据处理的时候,大部分时候也是处理对象相关的数据,所以对象在JS中是很重要的一个内容,也是必用的内容。本篇博文来分享一下关于在JS中对象里面添加对象的操作,虽然知识点不难,但是常用,所以总结一下存起来,方便查阅使用。通过本文的介绍,关于在JS中对象里面添加对象的操作就得心应手了,虽然该知识点不难但是重要和常用,尤其是对于刚接触前端开发不久的开发者来说更是如此,所以绝对掌握该知识点还是很有必要的,重要性不再赘述。欢迎关注,共同进步。_一个对象如何添加另一个对象

迁移学习在自然语言生成中的研究-程序员宅基地

文章浏览阅读257次,点赞3次,收藏8次。1.背景介绍自然语言生成(Natural Language Generation, NLG)是一种将计算机理解的结构化信息转换为自然语言文本的技术。自然语言生成可以用于多种应用,如机器翻译、文本摘要、对话系统等。随着深度学习技术的发展,自然语言生成的表现力得到了显著提高。迁移学习(Transfer Learning)是一种机器学习技术,它可以将在一个任务上学到的知识应用于另一个相关任务。在...

随便推点

SpringBoot引入第三方jar包或本地jar包的处理方式_springboot idea 直接启动 target 第三方 jar 包-程序员宅基地

文章浏览阅读262次。在开发过程中有时会用到maven仓库里没有的jar包或者本地的jar包,这时没办法通过pom直接引入,那么该怎么解决呢一般有两种方法第一种是将本地jar包安装在本地maven库 第二种是将本地jar包放入项目目录中这篇文章主要讲第二种方式,这又分两种情况,一种是打包jar包,第二种是打包war包jar包 先看看jar包的结构 用压缩工具打开一个jar包 打包后jar包的路径在BOOT-INF\lib目录下 ..._springboot idea 直接启动 target 第三方 jar 包

软件压力测试图片60张,Win10 64位用鲁大师界面cpu温度60上下,显卡40多。用压力测试7-8分钟cpu75左右,...-程序员宅基地

文章浏览阅读1.4k次。CPU正常情况下45-65℃或更低,夏天或者玩游戏时,温度会高点,不超过80都属于正常温度。高于80℃时,需要采取措施:要检查CPU和风扇间的散热硅脂是否失效;更换CPU风扇;给风扇除尘;在通风或者空调间中使用机器。显卡温度:显卡一般是整个机箱里温度最高的硬件,常规下50-70℃(或更低),运行大型3D游戏或播放高清视频的时候,温度可达到100℃左右,一般高负载下不超过110℃均视为正常范畴。如有..._windows cpu gpu 压测

Mac系统制作U盘安装盘,不能识别U盘的情况_making disk bootable不动-程序员宅基地

文章浏览阅读2.3w次,点赞2次,收藏5次。遇到的问题:OS10.12系统,使用Mac系统自带的磁盘工具,通过恢复来制作的U盘安装盘,开机按住option键,没有U盘的这个选项。原因:使用磁盘工具恢复,没有创建启动文件,使用命令行能创建。U盘抹掉,分区名为1。sudo /Applications/Install\ OS\ X\ El\ Capitan.app/Contents/Resources/createinstal_making disk bootable不动

LOJ6089 小Y的背包计数问题 背包、根号分治-程序员宅基地

文章浏览阅读129次。题目传送门题意:给出$N$表示背包容量,且会给出$N$种物品,第$i$个物品大小为$i$,数量也为$i$,求装满这个背包的方案数,对$23333333$取模。$N \leq 10^5$$23333333=17 \times 1372549$竟然不是质数性质太不优秀了(雾直接跑背包$O(N^2)$,于是咱们考虑挖掘性质、分开计算发现当$i < \sqrt{N}$时就是一个多..._背包 根号

验证码-程序员宅基地

文章浏览阅读110次。用.net实现网站验证码功能 收藏 一、验证码简介验证码功能一般是用于防止批量注册的,不少网站为了防止用户利用机器人自动注册、登录、灌水,都采用了验证码技术。所谓验证码,就是将一串随机产生的数字或字母或符号或文字,生成一幅图片, 图片里加上一些干扰象素(防止OCR),由用户肉眼识别其中的验证码信息,输入表单提交网站验证,验证成功后才能使用某项功能。常见的验证码有如下几种: 1、纯..._验证码的样本标签,是5个字符,每个字符的可能 取值范围是'0'~'9'、'a'~'z'共36

ImportError: undefined symbol: cudaSetupArgument_undefinded symbol: cudasetupargument-程序员宅基地

文章浏览阅读2.7k次,点赞2次,收藏3次。ImportError: undefined symbol: cudaSetupArgumentubuntu16.04How to solve?Step1.pip install -U torchvision==0.4.0链接: link.Step2.Problem:ImportError: cannot import name ‘PILLOW_VERSION’ from ‘PI..._undefinded symbol: cudasetupargument