cxf Webservice 使用httpClient 调用
2015-10-08 16:26
281 查看
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; } }
相关文章推荐
- NAL(网络自适应层)
- 一份快速实用的 tcpdump 命令参考手册
- c++计算网络延迟毫秒数
- restful httpclient request中文乱码解决办法
- AsyncHttpClient 源码分析
- Android 基于OkHttp的UI层回调封装
- IIS HTTPS 禁用不安全的SSL2.0
- Android Volley网络框架的基本使用
- 解决:Could not load type 'System.ServiceModel.Activation.HttpModule' from assemb
- 网络寻址1
- http协议格式
- HTTP 协议头详解
- 创建贝叶斯网络
- httpwatch抓包工具的使用方法
- 在windows下自动重启网络设备
- CentOS下编译安装Apache(httpd)
- NAT(Network Address Translation,网络地址转换)
- HeroKu的HTTP API 设计指南(中文版)
- iOS9 Https技术预研
- Appium python client 网络连接相关 api 用法