HttpClientUtil.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package com.tld.util;
  2. import java.io.IOException;
  3. import java.net.URI;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import com.alibaba.fastjson.JSON;
  9. import com.alibaba.fastjson.JSONObject;
  10. import com.tld.mapper.ErrorMapper;
  11. import com.tld.model.Error;
  12. import org.apache.http.Header;
  13. import org.apache.http.NameValuePair;
  14. import org.apache.http.client.config.RequestConfig;
  15. import org.apache.http.client.entity.UrlEncodedFormEntity;
  16. import org.apache.http.client.methods.CloseableHttpResponse;
  17. import org.apache.http.client.methods.HttpGet;
  18. import org.apache.http.client.methods.HttpPost;
  19. import org.apache.http.client.utils.URIBuilder;
  20. import org.apache.http.entity.ContentType;
  21. import org.apache.http.entity.StringEntity;
  22. import org.apache.http.impl.client.CloseableHttpClient;
  23. import org.apache.http.impl.client.HttpClients;
  24. import org.apache.http.message.BasicNameValuePair;
  25. import org.apache.http.util.EntityUtils;
  26. import org.apache.http.HttpStatus;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import javax.annotation.Resource;
  29. import javax.swing.text.html.parser.Entity;
  30. public class HttpClientUtil {
  31. @Autowired
  32. private ErrorMapper errorMapper;
  33. public static String doGet(String url, Map<String, String> param) {
  34. // 创建Httpclient对象
  35. CloseableHttpClient httpclient = HttpClients.createDefault();
  36. //设置请求超时时间(各项超时参数具体含义链接)
  37. RequestConfig requestConfig = RequestConfig.custom()
  38. .setConnectTimeout(10000)
  39. .setConnectionRequestTimeout(10000)
  40. .setSocketTimeout(10000)
  41. .build();
  42. String resultString = "";
  43. CloseableHttpResponse response = null;
  44. try {
  45. // 创建uri
  46. URIBuilder builder = new URIBuilder(url);
  47. if (param != null) {
  48. for (String key : param.keySet()) {
  49. builder.addParameter(key, param.get(key));
  50. }
  51. }
  52. URI uri = builder.build();
  53. // 创建http GET请求
  54. HttpGet httpGet = new HttpGet(uri);
  55. //给这个请求设置请求配置
  56. httpGet.setConfig(requestConfig);
  57. httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2");
  58. // 执行请求
  59. response = httpclient.execute(httpGet);
  60. // 判断返回状态是否为200
  61. if (response.getStatusLine().getStatusCode() == 200) {
  62. resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
  63. }
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. } finally {
  67. try {
  68. if (response != null) {
  69. response.close();
  70. }
  71. httpclient.close();
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. }
  75. }
  76. return resultString;
  77. }
  78. public Map<String, Object> doPost(String url, JSONObject param) {
  79. // 创建Httpclient对象
  80. CloseableHttpClient httpClient = HttpClients.createDefault();
  81. //设置请求超时时间
  82. RequestConfig requestConfig = RequestConfig.custom()
  83. .setConnectTimeout(10000)
  84. .setConnectionRequestTimeout(10000)
  85. .setSocketTimeout(10000)
  86. .build();
  87. CloseableHttpResponse response = null;
  88. Map<String, Object> map = new HashMap<>();
  89. String resultString = null;
  90. try {
  91. // 创建Http Post请求
  92. HttpPost httpPost = new HttpPost(url);
  93. httpPost.setConfig(requestConfig);
  94. httpPost.setHeader("AuthType", "Anonymous");
  95. httpPost.setHeader("GSAppId", "BI");
  96. httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
  97. // 创建参数列表
  98. if (param != null) {
  99. //请求参数转JOSN字符串
  100. StringEntity entity = new StringEntity(param.toString(), "UTF-8");
  101. entity.setContentEncoding("UTF-8");
  102. entity.setContentType("application/json");
  103. httpPost.setEntity(entity);
  104. }
  105. response = httpClient.execute(httpPost);
  106. // 执行http请求
  107. String responseVal = EntityUtils.toString(response.getEntity(), "utf-8");
  108. JSONObject jsonObject = JSON.parseObject(responseVal);
  109. if(JSON.parseObject(jsonObject.get("result").toString()).get("State").equals("0")){
  110. //如果失败存入报错信息跟数据
  111. Error error = new Error()
  112. .setErrorInfo(resultString)
  113. .setUrl(url)
  114. .setDataVal(param.toString());
  115. errorMapper.addError(error);
  116. }
  117. resultString = EntityUtils.toString(response.getEntity(), "utf-8");
  118. map.put("msg", response.getStatusLine().getStatusCode());
  119. map.put("data", resultString);
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. } finally {
  123. try {
  124. response.close();
  125. } catch (IOException e) {
  126. // TODO Auto-generated catch block
  127. e.printStackTrace();
  128. }
  129. }
  130. return map;
  131. }
  132. public static String doPostJson(String url, String json) {
  133. // 创建Httpclient对象
  134. CloseableHttpClient httpClient = HttpClients.createDefault();
  135. //设置请求超时时间
  136. RequestConfig requestConfig = RequestConfig.custom()
  137. .setConnectTimeout(10000)
  138. .setConnectionRequestTimeout(10000)
  139. .setSocketTimeout(10000)
  140. .build();
  141. CloseableHttpResponse response = null;
  142. String resultString = "";
  143. try {
  144. // 创建Http Post请求
  145. HttpPost httpPost = new HttpPost(url);
  146. httpPost.setConfig(requestConfig);
  147. // 创建请求内容 ,发送json数据需要设置contentType
  148. StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
  149. httpPost.setEntity(entity);
  150. // 执行http请求
  151. response = httpClient.execute(httpPost);
  152. if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
  153. resultString = EntityUtils.toString(response.getEntity(), "utf-8");
  154. }
  155. } catch (Exception e) {
  156. e.printStackTrace();
  157. } finally {
  158. try {
  159. response.close();
  160. } catch (IOException e) {
  161. // TODO Auto-generated catch block
  162. e.printStackTrace();
  163. }
  164. }
  165. return resultString;
  166. }
  167. }