HttpHelp
来源:互联网 发布:淘宝店铺出售可靠吗 编辑:程序博客网 时间:2024/06/03 17:10
import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpStatus;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.methods.StringRequestEntity;import org.apache.commons.httpclient.methods.multipart.FilePart;import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;import org.apache.commons.httpclient.methods.multipart.Part;import org.apache.commons.httpclient.params.HttpMethodParams;
/**<span style="white-space:pre"></span> * @throws Exception <span style="white-space:pre"></span>* @Title: postRequestResponseBodyAsString <span style="white-space:pre"></span>* @Description: http post请求,application/json;charset=UTF-8方式<span style="white-space:pre"></span>* @param url<span style="white-space:pre"></span>* @param str<span style="white-space:pre"></span>* @param contimeout<span style="white-space:pre"></span>* @param sotimeout<span style="white-space:pre"></span>* @return<span style="white-space:pre"></span>* @throws Exception<span style="white-space:pre"></span>* @throws <span style="white-space:pre"></span>*/
public static String postRequestResponseBodyAsString(String url, String str,Integer contimeout,Integer sotimeout) throws Exception{ HttpClient httpClient = new HttpClient(); httpClient.getHttpConnectionManager().getParams().setSoTimeout(sotimeout); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(contimeout); PostMethod method = new PostMethod(url); int statusCode; String responseCharSet =""; String responseString = ""; try {// String json = JSON.toJSONString(o); StringRequestEntity entity = new StringRequestEntity(str,"application/json","utf-8");//解决中文乱码问题 method.setRequestEntity(entity); method.addRequestHeader("Content-Type", "application/json;charset=UTF-8"); statusCode = httpClient.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { LOG.error(">>>>>>>>>>>>>> Method failed: " + method.getStatusLine()); LOG.error(">>>>>>>>>>>>>> Http服务链路异常:服务器状态码为" + statusCode); } responseCharSet = method.getResponseCharSet(); responseString = method.getResponseBodyAsString(); if ("ISO-8859-1".equals(responseCharSet)) { responseString = new String(responseString.getBytes(responseCharSet), "UTF-8"); } } catch (Exception e) { LOG.error(">>>>>>>>>>>>>> Http服务链路异常:" + e.getMessage() + e); } finally { method.releaseConnection(); } return responseString; }
<span style="white-space:pre"></span>public static String postRequestResponseBodyAsString(String url, Map<String, String> parameters) throws Exception {<span style="white-space:pre"></span>return postRequestResponseBodyAsString(url, parameters, 8000, 20000);<span style="white-space:pre"></span>}
/** * @Title: uploadFile * @Description: 上传文件 * @param file 文件 * @param url 上传地址 * @param map header-type * @return * @throws */ public static String uploadFile(File file, String url,Map<String, String> map) { if (!file.exists()) { return null; } PostMethod postMethod = new PostMethod(url); String responseCharSet =""; String responseString = ""; try { // FilePart:用来上传文件的类 FilePart fp = new CustomFilePart("filedata", file); Part[] parts = { fp }; postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,FilePart.DEFAULT_CHARSET); // 对于MIME类型的请求,httpclient建议全用MulitPartRequestEntity进行包装 MultipartRequestEntity mre = new MultipartRequestEntity(parts, postMethod.getParams()); postMethod.setRequestEntity(mre); for (String key : map.keySet()) { postMethod.addRequestHeader(key, map.get(key)); } HttpClient httpClient = new HttpClient(); httpClient.getHttpConnectionManager().getParams().setSoTimeout(20000); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(8000); int status = httpClient.executeMethod(postMethod); responseCharSet = postMethod.getResponseCharSet(); responseString = postMethod.getResponseBodyAsString(); if (status == HttpStatus.SC_OK) { if ("ISO-8859-1".equals(responseCharSet)) { responseString = new String(responseString.getBytes(responseCharSet), "UTF-8"); } } else { LOG.error(">>>>>>>>>>>>>> Method failed: " + postMethod.getStatusLine()); LOG.error(">>>>>>>>>>>>>> Http服务链路异常:服务器状态码为" + status); } } catch (Exception e) { e.printStackTrace(); } finally { // 释放连接 postMethod.releaseConnection(); } return responseString; }
/** * @Title: getFile * @Description: 拿到文件内容 * @param url 地址 * @param charset 编码 * @return * @throws */ public static String getFile(String url,Charset charset) { return getFile(url, charset,8000,20000); } public static String getFile(String url,Charset charset,Integer contimeout,Integer sotimeout) { GetMethod method = new GetMethod(url); String responseCharSet =""; String responseString = ""; try { HttpClient httpClient = new HttpClient(); httpClient.getHttpConnectionManager().getParams().setSoTimeout(sotimeout); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(contimeout); int status = httpClient.executeMethod(method); responseCharSet = method.getResponseCharSet(); responseString = method.getResponseBodyAsString(); if (status == HttpStatus.SC_OK) { if ("ISO-8859-1".equals(responseCharSet)) { responseString = new String(responseString.getBytes(responseCharSet), charset); } } else { LOG.error(">>>>>>>>>>>>>> Method failed: " + method.getStatusLine()); LOG.error(">>>>>>>>>>>>>> Http服务链路异常:服务器状态码为" + status); } } catch (Exception e) { e.printStackTrace(); } finally { // 释放连接 method.releaseConnection(); } return responseString; }
0 0
- httphelp
- HttpHelp
- MFC工程从VC6.0转换成VS2013遇到的问题以及解决方法
- web.xml 配置详解
- 字符串匹配的KMP算法
- 列表滑动删除
- Windows10 下如何git库的 ssh 公钥(适用github,公司git库管理)
- HttpHelp
- SQL Server存储引擎 - 01. 数据库如何读写数据
- Calculate S(n) (规律题)
- 正则表达式学习笔记
- 剑指Offer:二叉树的镜像
- HDU1240 Asteroids!(BFS)
- 分治法讲解及实例
- 关于通知Notification的基础知识
- Asp.Net 应用程序在IIS发布后无法连接oracle数据库问题的解决方法