httpClient学习整理

Posted edgedance

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpClient学习整理相关的知识,希望对你有一定的参考价值。

  1. package com.httpclient;
  2. import java.io.IOException;
  3. import java.net.URI;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.apache.http.NameValuePair;
  8. import org.apache.http.client.entity.UrlEncodedFormEntity;
  9. import org.apache.http.client.methods.CloseableHttpResponse;
  10. import org.apache.http.client.methods.HttpGet;
  11. import org.apache.http.client.methods.HttpPost;
  12. import org.apache.http.client.utils.URIBuilder;
  13. import org.apache.http.entity.ContentType;
  14. import org.apache.http.entity.StringEntity;
  15. import org.apache.http.impl.client.CloseableHttpClient;
  16. import org.apache.http.impl.client.HttpClients;
  17. import org.apache.http.message.BasicNameValuePair;
  18. import org.apache.http.util.EntityUtils;
  19. /**
  20. * httpClient工具类
  21. * @author jianghui
  22. * 百度搜索这个关键字,下载jar包: httpcomponents-client-4.5.3
  23. */
  24. public class HttpClientUtil {
  25. public static void main(String[] args) {
  26. String doGet = doGet("http://www.baidu.com");
  27. System.out.println(doGet);
  28. }
  29. public static String doGet(String url, Map<String, String> param) {
  30. // 创建Httpclient对象
  31. CloseableHttpClient httpclient = HttpClients.createDefault();
  32. String resultString = "";
  33. CloseableHttpResponse response = null;
  34. try {
  35. // 创建uri
  36. URIBuilder builder = new URIBuilder(url);
  37. if (param != null) {
  38. for (String key : param.keySet()) {
  39. builder.addParameter(key, param.get(key));
  40. }
  41. }
  42. URI uri = builder.build();
  43. // 创建http GET请求
  44. HttpGet httpGet = new HttpGet(uri);
  45. // 执行请求
  46. response = httpclient.execute(httpGet);
  47. // 判断返回状态是否为200
  48. if (response.getStatusLine().getStatusCode() == 200) {
  49. resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
  50. }
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. } finally {
  54. try {
  55. if (response != null) {
  56. response.close();
  57. }
  58. httpclient.close();
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. return resultString;
  64. }
  65. public static String doGet(String url) {
  66. return doGet(url, null);
  67. }
  68. public static String doPost(String url, Map<String, String> param) {
  69. // 创建Httpclient对象
  70. CloseableHttpClient httpClient = HttpClients.createDefault();
  71. CloseableHttpResponse response = null;
  72. String resultString = "";
  73. try {
  74. // 创建Http Post请求
  75. HttpPost httpPost = new HttpPost(url);
  76. // 创建参数列表
  77. if (param != null) {
  78. List<NameValuePair> paramList = new ArrayList<>();
  79. for (String key : param.keySet()) {
  80. paramList.add(new BasicNameValuePair(key, param.get(key)));
  81. }
  82. // 模拟表单
  83. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
  84. httpPost.setEntity(entity);
  85. }
  86. // 执行http请求
  87. response = httpClient.execute(httpPost);
  88. resultString = EntityUtils.toString(response.getEntity(), "utf-8");
  89. } catch (Exception e) {
  90. e.printStackTrace();
  91. } finally {
  92. try {
  93. response.close();
  94. } catch (IOException e) {
  95. // TODO Auto-generated catch block
  96. e.printStackTrace();
  97. }
  98. }
  99. return resultString;
  100. }
  101. public static String doPost(String url) {
  102. return doPost(url, null);
  103. }
  104. public static String doPostJson(String url, String json) {
  105. // 创建Httpclient对象
  106. CloseableHttpClient httpClient = HttpClients.createDefault();
  107. CloseableHttpResponse response = null;
  108. String resultString = "";
  109. try {
  110. // 创建Http Post请求
  111. HttpPost httpPost = new HttpPost(url);
  112. // 创建请求内容
  113. StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
  114. httpPost.setEntity(entity);
  115. // 执行http请求
  116. response = httpClient.execute(httpPost);
  117. resultString = EntityUtils.toString(response.getEntity(), "utf-8");
  118. } catch (Exception e) {
  119. e.printStackTrace();
  120. } finally {
  121. try {
  122. response.close();
  123. } catch (IOException e) {
  124. // TODO Auto-generated catch block
  125. e.printStackTrace();
  126. }
  127. }
  128. return resultString;
  129. }
  130. }

以上是关于httpClient学习整理的主要内容,如果未能解决你的问题,请参考以下文章

HttpClient学习整理

HttpClient学习整理

HttpClient学习整理

httpClient学习整理

httpClient学习整理

工具类代码整理