httpcomponents之httpclient发送http请求

蔚落 2022-09-25 10:25 277阅读 0赞

Commons的HttpClient项目现在是生命的尽头,不再被开发。它已取代由Apache HttpComponents项目HttpClient和的HttpCore模组,提供更好的性能和更大的灵活性。

pom使用4.1.2

  1. <span style="white-space:pre"> </span><dependency>
  2. <groupId>org.apache.httpcomponents</groupId>
  3. <artifactId>httpclient</artifactId>
  4. <version>4.1.2</version>
  5. </dependency>

httpclient类:

  1. import java.io.IOException;
  2. import java.io.UnsupportedEncodingException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Set;
  7. import java.util.logging.Logger;
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.NameValuePair;
  11. import org.apache.http.ParseException;
  12. import org.apache.http.client.ClientProtocolException;
  13. import org.apache.http.client.entity.UrlEncodedFormEntity;
  14. import org.apache.http.client.methods.HttpGet;
  15. import org.apache.http.client.methods.HttpPost;
  16. import org.apache.http.client.methods.HttpUriRequest;
  17. import org.apache.http.impl.client.DefaultHttpClient;
  18. import org.apache.http.message.BasicNameValuePair;
  19. import org.apache.http.protocol.HTTP;
  20. import org.apache.http.util.EntityUtils;
  21. public class HttpClient {
  22. /**
  23. * 发送post请求
  24. * @param url
  25. * @param params
  26. * @return
  27. */
  28. public static String post(String url, Map<String, String> params) {
  29. DefaultHttpClient httpclient = new DefaultHttpClient();
  30. String body = null;
  31. HttpPost post = postForm(url, params);
  32. body = invoke(httpclient, post);
  33. httpclient.getConnectionManager().shutdown();
  34. return body;
  35. }
  36. /**
  37. * 发送get请求
  38. * @param url
  39. * @return
  40. */
  41. public static String get(String url) {
  42. DefaultHttpClient httpclient = new DefaultHttpClient();
  43. String body = null;
  44. HttpGet get = new HttpGet(url);
  45. body = invoke(httpclient, get);
  46. httpclient.getConnectionManager().shutdown();
  47. return body;
  48. }
  49. private static String invoke(DefaultHttpClient httpclient, HttpUriRequest httpost) {
  50. HttpResponse response = sendRequest(httpclient, httpost);
  51. String body = paseResponse(response);
  52. return body;
  53. }
  54. private static String paseResponse(HttpResponse response) {
  55. HttpEntity entity = response.getEntity();
  56. String charset = EntityUtils.getContentCharSet(entity);
  57. String body = null;
  58. try {
  59. body = EntityUtils.toString(entity);
  60. } catch (ParseException e) {
  61. e.printStackTrace();
  62. } catch (IOException e) {
  63. e.printStackTrace();
  64. }
  65. return body;
  66. }
  67. private static HttpResponse sendRequest(DefaultHttpClient httpclient, HttpUriRequest httpost) {
  68. HttpResponse response = null;
  69. try {
  70. response = httpclient.execute(httpost);
  71. } catch (ClientProtocolException e) {
  72. e.printStackTrace();
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. return response;
  77. }
  78. private static HttpPost postForm(String url, Map<String, String> params) {
  79. HttpPost httpost = new HttpPost(url);
  80. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  81. // 需要传一个token
  82. //httpost.setHeader("token", "c7a4e021-6527-11e6-96be-fcaa14c3021a1");
  83. Set<String> keySet = params.keySet();
  84. for (String key : keySet) {
  85. nvps.add(new BasicNameValuePair(key, params.get(key)));
  86. }
  87. try {
  88. httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
  89. } catch (UnsupportedEncodingException e) {
  90. e.printStackTrace();
  91. }
  92. return httpost;
  93. }
  94. }

发表评论

表情:
评论列表 (有 0 条评论,277人围观)

还没有评论,来说两句吧...

相关阅读