HttpClientUtils.java 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package com.boman.wechat.utils;
  2. import org.apache.http.NameValuePair;
  3. import org.apache.http.client.config.RequestConfig;
  4. import org.apache.http.client.entity.UrlEncodedFormEntity;
  5. import org.apache.http.client.methods.CloseableHttpResponse;
  6. import org.apache.http.client.methods.HttpGet;
  7. import org.apache.http.client.methods.HttpPost;
  8. import org.apache.http.impl.client.CloseableHttpClient;
  9. import org.apache.http.impl.client.HttpClients;
  10. import org.apache.http.message.BasicNameValuePair;
  11. import org.apache.http.util.EntityUtils;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.Map;
  16. public class HttpClientUtils {
  17. final static int TIMEOUT = 1000;
  18. final static int TIMEOUT_MSEC = 5 * 1000;
  19. public static String doPost(String url, Map<String, String> paramMap) throws IOException {
  20. // 创建Httpclient对象
  21. CloseableHttpClient httpClient = HttpClients.createDefault();
  22. CloseableHttpResponse response = null;
  23. String resultString = "";
  24. try {
  25. // 创建Http Post请求
  26. HttpPost httpPost = new HttpPost(url);
  27. // 创建参数列表
  28. if (paramMap != null) {
  29. List<NameValuePair> paramList = new ArrayList<>();
  30. for (Map.Entry<String, String> param : paramMap.entrySet()) {
  31. paramList.add(new BasicNameValuePair(param.getKey(), param.getValue()));
  32. }
  33. // 模拟表单
  34. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
  35. httpPost.setEntity(entity);
  36. }
  37. httpPost.setConfig(builderRequestConfig());
  38. // 执行http请求
  39. response = httpClient.execute(httpPost);
  40. resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
  41. } catch (Exception e) {
  42. throw e;
  43. } finally {
  44. try {
  45. response.close();
  46. } catch (IOException e) {
  47. throw e;
  48. }
  49. }
  50. return resultString;
  51. }
  52. public static String doGet(String url, Map<String, String> paramMap) throws IOException {
  53. url += "?appid=" + paramMap.get("appid") + "&secret=" + paramMap.get("secret") + "&js_code=" + paramMap.get("js_code") + "&grant_type=" + paramMap.get("grant_type");
  54. // 创建Httpclient对象
  55. CloseableHttpClient httpClient = HttpClients.createDefault();
  56. CloseableHttpResponse response = null;
  57. String resultString = "";
  58. try {
  59. // 创建Http Post请求
  60. HttpGet httpGet = new HttpGet(url);
  61. // 执行http请求
  62. response = httpClient.execute(httpGet);
  63. resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
  64. } catch (Exception e) {
  65. throw e;
  66. } finally {
  67. try {
  68. response.close();
  69. } catch (IOException e) {
  70. throw e;
  71. }
  72. }
  73. return resultString;
  74. }
  75. private static RequestConfig builderRequestConfig() {
  76. return RequestConfig.custom()
  77. .setConnectTimeout(TIMEOUT_MSEC)
  78. .setConnectionRequestTimeout(TIMEOUT_MSEC)
  79. .setSocketTimeout(TIMEOUT_MSEC).build();
  80. }
  81. }