1. package com.j1.weixin.util;
  2.  
  3. import java.io.IOException;
  4. import java.util.Map;
  5. import java.util.Set;
  6.  
  7. import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
  8. import org.apache.commons.httpclient.HttpClient;
  9. import org.apache.commons.httpclient.HttpException;
  10. import org.apache.commons.httpclient.HttpStatus;
  11. import org.apache.commons.httpclient.NameValuePair;
  12. import org.apache.commons.httpclient.methods.GetMethod;
  13. import org.apache.commons.httpclient.methods.PostMethod;
  14. import org.apache.commons.httpclient.params.HttpMethodParams;
  15. import org.apache.log4j.Logger;
  16.  
  17. public class HttpUtils {
  18. /**
  19. * 发送HTTP请求
  20. *
  21. * @param url
  22. * @param propsMap 发送的参数
  23. */
  24. public static HttpResponse httpPost(String url, Map<String, Object> propsMap) {
  25. HttpResponse response = new HttpResponse();
  26. String responseMsg = "";
  27.  
  28. HttpClient httpClient = new HttpClient();
  29. PostMethod postMethod = new PostMethod(url);// POST请求
  30. if (propsMap != null) {
  31. // 参数设置
  32. Set<String> keySet = propsMap.keySet();
  33. NameValuePair[] postData = new NameValuePair[keySet.size()];
  34. int index = 0;
  35. for (String key : keySet) {
  36. postData[index++] = new NameValuePair(key, propsMap.get(key)
  37. .toString());
  38. }
  39. postMethod.addParameters(postData);
  40. }
  41. postMethod.getParams().setParameter(
  42. HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
  43. postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
  44. try {
  45. int statusCode = httpClient.executeMethod(postMethod);// 发送请求
  46. response.setStatusCode(statusCode);
  47. if (statusCode == HttpStatus.SC_OK) {
  48. // 读取内容
  49. byte[] responseBody = postMethod.getResponseBody();
  50. // 处理返回的内容
  51. responseMsg = new String(responseBody, "utf-8");
  52. }
  53. } catch (HttpException e) {
  54. e.printStackTrace();
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. } finally {
  58. postMethod.releaseConnection();// 关闭连接
  59. }
  60. response.setContent(responseMsg);
  61. return response;
  62. }
  63.  
  64. /**
  65. * 发送HTTP请求
  66. *
  67. * @param url
  68. */
  69. public static HttpResponse httpGet(String url) {
  70. HttpResponse response = new HttpResponse();
  71. String responseMsg = "";
  72.  
  73. HttpClient httpClient = new HttpClient();
  74. GetMethod getMethod = new GetMethod(url);
  75.  
  76. try {
  77. int statusCode = httpClient.executeMethod(getMethod);// 发送请求
  78. response.setStatusCode(statusCode);
  79. if (statusCode == HttpStatus.SC_OK) {
  80. // 读取内容
  81. byte[] responseBody = getMethod.getResponseBody();
  82. // 处理返回的内容
  83. responseMsg = new String(responseBody, "utf-8");
  84. }
  85. } catch (HttpException e) {
  86. e.printStackTrace();
  87. } catch (IOException e) {
  88. e.printStackTrace();
  89. } finally {
  90. getMethod.releaseConnection();// 关闭连接
  91. }
  92. response.setContent(responseMsg);
  93. return response;
  94. }
  95.  
  96. /**
  97. * 发送HTTP--GET请求
  98. *
  99. * @param url
  100. * @param propsMap
  101. * 发送的参数
  102. */
  103. public static String httpGetSend(String url) {
  104. String responseMsg = "";
  105. HttpClient httpClient = new HttpClient();
  106. GetMethod getMethod = new GetMethod(url);// GET请求
  107. try {
  108. // http超时5秒
  109. httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
  110. // 设置 get 请求超时为 5 秒
  111. getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
  112. getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
  113. httpClient.executeMethod(getMethod);// 发送请求
  114. // 读取内容
  115. byte[] responseBody = getMethod.getResponseBody();
  116. // 处理返回的内容
  117. responseMsg = new String(responseBody, "utf-8");
  118. } catch (Exception e) {
  119. Logger.getLogger(HttpUtils.class).error(e.getMessage());
  120. e.printStackTrace();
  121. } finally {
  122. getMethod.releaseConnection();// 关闭连接
  123. }
  124. return responseMsg;
  125. }
  126.  
  127. /**
  128. * 发送HTTP请求
  129. *
  130. * @param url
  131. * @param propsMap
  132. * 发送的参数
  133. */
  134. public static String httpSend(String url, Map<String, Object> propsMap) {
  135. String responseMsg = "";
  136.  
  137. HttpClient httpClient = new HttpClient();
  138.  
  139. PostMethod postMethod = new PostMethod(url);// POST请求
  140.  
  141. // postMethod.
  142. // 参数设置
  143. Set<String> keySet = propsMap.keySet();
  144. NameValuePair[] postData = new NameValuePair[keySet.size()];
  145. int index = 0;
  146. for (String key : keySet) {
  147. postData[index++] = new NameValuePair(key, propsMap.get(key)
  148. .toString());
  149. }
  150. postMethod.addParameters(postData);
  151. try {
  152. httpClient.executeMethod(postMethod);// 发送请求
  153. // Log.info(postMethod.getStatusCode());
  154. // 读取内容
  155. byte[] responseBody = postMethod.getResponseBody();
  156. // 处理返回的内容
  157. responseMsg = new String(responseBody);
  158.  
  159. } catch (HttpException e) {
  160. e.printStackTrace();
  161. } catch (IOException e) {
  162. e.printStackTrace();
  163. } finally {
  164. postMethod.releaseConnection();// 关闭连接
  165. }
  166. return responseMsg;
  167. }
  168.  
  169. /**
  170. * 发送Post HTTP请求
  171. *
  172. * @param url
  173. * @param propsMap
  174. * 发送的参数
  175. * @throws IOException
  176. * @throws HttpException
  177. */
  178. public static PostMethod httpSendPost(String url, Map<String, Object> propsMap,String authrition) throws Exception {
  179. HttpClient httpClient = new HttpClient();
  180. PostMethod postMethod = new PostMethod(url);// POST请求
  181. postMethod.addRequestHeader("Authorization",authrition);
  182. postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
  183. // 参数设置
  184. Set<String> keySet = propsMap.keySet();
  185. NameValuePair[] postData = new NameValuePair[keySet.size()];
  186. int index = 0;
  187. for (String key : keySet) {
  188. postData[index++] = new NameValuePair(key, propsMap.get(key)
  189. .toString());
  190. }
  191. postMethod.addParameters(postData);
  192.  
  193. httpClient.executeMethod(postMethod);// 发送请求
  194. return postMethod;
  195. }
  196.  
  197. /**
  198. * 发送Post HTTP请求
  199. *
  200. * @param url
  201. * @param propsMap
  202. * 发送的参数
  203. * @throws IOException
  204. * @throws HttpException
  205. */
  206. public static PostMethod httpSendPost(String url, Map<String, Object> propsMap) throws Exception {
  207. String responseMsg = "";
  208. HttpClient httpClient = new HttpClient();
  209. PostMethod postMethod = new PostMethod(url);// POST请求
  210. postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
  211. // 参数设置
  212. Set<String> keySet = propsMap.keySet();
  213. NameValuePair[] postData = new NameValuePair[keySet.size()];
  214. int index = 0;
  215. for (String key : keySet) {
  216. postData[index++] = new NameValuePair(key, propsMap.get(key)
  217. .toString());
  218. }
  219. postMethod.addParameters(postData);
  220.  
  221. httpClient.executeMethod(postMethod);// 发送请求
  222. return postMethod;
  223. }
  224.  
  225. /**
  226. * 发送Get HTTP请求
  227. *
  228. * @param url
  229. * @param propsMap
  230. * 发送的参数
  231. * @throws IOException
  232. * @throws HttpException
  233. */
  234. public static GetMethod httpSendGet(String url, Map<String, Object> propsMap) throws Exception {
  235. String responseMsg = "";
  236. HttpClient httpClient = new HttpClient();
  237. GetMethod getMethod = new GetMethod(url);// GET请求
  238. getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
  239. // 参数设置
  240. Set<String> keySet = propsMap.keySet();
  241. NameValuePair[] postData = new NameValuePair[keySet.size()];
  242. int index = 0;
  243. for (String key : keySet) {
  244. getMethod.getParams().setParameter(key, propsMap.get(key)
  245. .toString());
  246. }
  247.  
  248. httpClient.executeMethod(getMethod);// 发送请求
  249. return getMethod;
  250. }
  251.  
  252. }

post 封装Map 发送请求的更多相关文章

  1. Python 使用 requests 模块发送请求的使用及封装

    一.requests 模块基本使用 1.准备接口的URL.请求参数.请求头 # 1. 构造注册.登录.充值请求的url register_url = "注册url" login_u ...

  2. Java爬虫(一)利用GET和POST发送请求,获取服务器返回信息

    本人所使用软件 eclipse fiddle UC浏览器 分析请求信息 以知乎(https://www.zhihu.com)为例,模拟登陆请求,获取登陆后首页,首先就是分析请求信息. 用UC浏览器F1 ...

  3. http post发送请求

    一: 用java自带URL发送 public synchronized JSONObject getJSON(String url2, String param) { try { URL url = ...

  4. consumer发送请求,接收响应

    一般情况,consumer发送请求时,创建一个DefaultFuture对象,然后阻塞并等待响应.DefaultFuture类,封装了请求和响应: // 省略其他代码 public class Def ...

  5. 九、封装登录POST请求、登录后POST请求以及GET请求

    一.封装登录后POST请求以及GET请求 /** * 全局运行时环境参数管理器 */ public static Map<String, String> BASE_GLOBAL_MAP; ...

  6. RestTemplate发送请求并携带header信息

    1.使用restTemplate的postForObject方法 注:目前没有发现发送携带header信息的getForObject方法. HttpHeaders headers = new Http ...

  7. 封装的ajax请求

    在做登录注册这类提交表单数据时,我们经常需要局部刷新网页来验证用户输入的信息,这就需要用到ajax请求,我们通常需要获取表单中的数据,发起ajax请求,通过服务程序,与数据库的数据进行比对,判断信息的 ...

  8. ajax-向服务器发送请求

    ajax-向服务器发送请求 1.将请求发送到服务器,使用XMLHttpRequest对象的 open() 和 send() 方法.     xmlhttp. open(method,url,async ...

  9. Android 网络请求库volley的封装,让请求更方便

    首先封装一下volley 请求 public class CustomRequest extends StringRequest { private static final String TAG = ...

随机推荐

  1. 接受POST表单传过来的信息 可以用foreach循环进行遍历操作

    if(isset($_POST['Goods'])){                     foreach($_POST['Goods'] as $_k =>$_v){            ...

  2. C# ERP开发框架

    C/S系统开发框架-企业版 V4.0 (Enterprise Edition) 简介: http://www.csframework.com/cs-framework-4.0.htm 视频下载: 百度 ...

  3. java单点登录系统CAS的简单使用

    转:http://blog.csdn.net/yunye114105/article/details/7997041 背景 有几个相对独立的java的web应用系统, 各自有自己的登陆验证功能,用户在 ...

  4. 关于 self 和 super 在oc 中 的疑惑 与 分析

    关于 self 和 super 在oc 中 的疑惑 与 分析   面试一定都是很注重 基础的,不管高级还是初级. 虽然基础好跟基础不好都可以写 代码,网上那么多资料.  区分高低也就是研究的深度和广度 ...

  5. Ruby自学笔记(二)— Ruby的一些基础知识

    Ruby安装好之后,我们就可以来实践Ruby语言了. 以下是一些学习到的简单基础知识: 1. 如何执行Ruby文件? 我们编写的Ruby文件是以rb为后缀名的,例如:XXX.rb.当要执行ruby文件 ...

  6. PHP与MYSQL配合完成IP的存取

    如何存储IP 程序设计要在功能实现的基础上最大限度的优化性能.而数据库设计是程序设计中不可忽略的重要部分,巧存IP地址可以一定程度提升性能. 利用函数算法处理 MySQL没有直接提供IP类型字段,但有 ...

  7. pycharm去掉拼写检查

    http://zhidao.baidu.com/question/523436629.html

  8. ubuntu進入dos界面命令 ubuntu進入圖形界面命令

    切换界面: ctrl + alt + F1是切到终端模式 Alt+F7 切到图形界面

  9. 关于javascript document.createDocumentFragment() 替代insertCell、insertRow这种每次都使用大量的资源导致浏览器崩溃

    documentFragment 是一個無父對象的document對象他支持以下DOM2方法: appendChild, cloneNode, hasAttributes, hasChildNodes ...

  10. Ubuntu 12.04 下安装 Eclipse

    方法一:(缺点是安装时附加openjdk等大量程序并无法去除,优点是安装简单) $ sudo apt-get install eclipse 方法二:(优点是安装内容清爽,缺点是配置麻烦)1.安装JD ...