[关闭]
@awsekfozc 2017-07-19T01:58:28.000000Z 字数 3413 阅读 1161

HttpClientService

httpclient


  1. import org.apache.http.NameValuePair;
  2. import org.apache.http.client.ClientProtocolException;
  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.client.utils.URIBuilder;
  9. import org.apache.http.entity.ContentType;
  10. import org.apache.http.entity.StringEntity;
  11. import org.apache.http.impl.client.CloseableHttpClient;
  12. import org.apache.http.message.BasicNameValuePair;
  13. import org.apache.http.util.EntityUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. import java.io.IOException;
  17. import java.net.URISyntaxException;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Map;
  21. @Service
  22. public class HttpClientService {
  23. @Autowired
  24. private CloseableHttpClient httpClient;
  25. @Autowired
  26. private RequestConfig requestConfig;
  27. /**
  28. * 执行GET请求
  29. *
  30. * @param url
  31. * @return
  32. * @throws IOException
  33. * @throws ClientProtocolException
  34. */
  35. public String doGet(String url) throws ClientProtocolException, IOException {
  36. // 创建http GET请求
  37. HttpGet httpGet = new HttpGet(url);
  38. httpGet.setConfig(this.requestConfig);
  39. CloseableHttpResponse response = null;
  40. try {
  41. // 执行请求
  42. response = httpClient.execute(httpGet);
  43. // 判断返回状态是否为200
  44. if (response.getStatusLine().getStatusCode() == 200) {
  45. return EntityUtils.toString(response.getEntity(), "UTF-8");
  46. }
  47. } finally {
  48. if (response != null) {
  49. response.close();
  50. }
  51. }
  52. return null;
  53. }
  54. /**
  55. * 带有参数的GET请求
  56. *
  57. * @param url
  58. * @param params
  59. * @return
  60. * @throws URISyntaxException
  61. * @throws IOException
  62. * @throws ClientProtocolException
  63. */
  64. public String doGet(String url, Map<String, String> params)
  65. throws ClientProtocolException, IOException, URISyntaxException {
  66. URIBuilder uriBuilder = new URIBuilder(url);
  67. for (String key : params.keySet()) {
  68. uriBuilder.addParameter(key, params.get(key));
  69. }
  70. return this.doGet(uriBuilder.build().toString());
  71. }
  72. /**
  73. * 执行POST请求
  74. *
  75. * @param url
  76. * @param params
  77. * @return
  78. * @throws IOException
  79. */
  80. public String doPost(String url, Map<String, String> params) throws IOException {
  81. // 创建http POST请求
  82. HttpPost httpPost = new HttpPost(url);
  83. httpPost.setConfig(this.requestConfig);
  84. if (params != null) {
  85. // 设置2个post参数,一个是scope、一个是q
  86. List<NameValuePair> parameters = new ArrayList<NameValuePair>();
  87. for (String key : params.keySet()) {
  88. parameters.add(new BasicNameValuePair(key, params.get(key)));
  89. }
  90. // 构造一个form表单式的实体
  91. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, "UTF-8");
  92. // 将请求实体设置到httpPost对象中
  93. httpPost.setEntity(formEntity);
  94. }
  95. CloseableHttpResponse response = null;
  96. try {
  97. // 执行请求
  98. response = httpClient.execute(httpPost);
  99. return EntityUtils.toString(response.getEntity(), "UTF-8");
  100. } finally {
  101. if (response != null) {
  102. response.close();
  103. }
  104. }
  105. }
  106. /**
  107. * 执行POST请求
  108. *
  109. * @param url
  110. * @return
  111. * @throws IOException
  112. */
  113. public String doPost(String url) throws IOException {
  114. return this.doPost(url, null);
  115. }
  116. /**
  117. * 提交json数据
  118. *
  119. * @param url
  120. * @param json
  121. * @return
  122. * @throws ClientProtocolException
  123. * @throws IOException
  124. */
  125. public String doPostJson(String url, String json) throws ClientProtocolException, IOException {
  126. // 创建http POST请求
  127. HttpPost httpPost = new HttpPost(url);
  128. httpPost.setConfig(this.requestConfig);
  129. if (json != null) {
  130. // 构造一个form表单式的实体
  131. StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
  132. // 将请求实体设置到httpPost对象中
  133. httpPost.setEntity(stringEntity);
  134. }
  135. CloseableHttpResponse response = null;
  136. try {
  137. // 执行请求
  138. response = this.httpClient.execute(httpPost);
  139. return EntityUtils.toString(response.getEntity(), "UTF-8");
  140. } finally {
  141. if (response != null) {
  142. response.close();
  143. }
  144. }
  145. }
  146. }

在此输入正文

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注