1. 程式人生 > >httpClient學習整理

httpClient學習整理

list out httppost com 列表 set 模擬 pack except

  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學習整理