1. 程式人生 > 實用技巧 >HTTP請求工具類封裝

HTTP請求工具類封裝

  1 import com.alibaba.fastjson.JSONObject;
  2 import org.apache.http.HttpEntity;
  3 import org.apache.http.client.config.RequestConfig;
  4 import org.apache.http.client.methods.HttpGet;
  5 import org.apache.http.client.methods.HttpPost;
  6 import org.apache.http.client.methods.HttpRequestBase;
7 import org.apache.http.entity.StringEntity; 8 import org.apache.http.impl.client.CloseableHttpClient; 9 import org.apache.http.impl.client.HttpClients; 10 import org.apache.http.util.EntityUtils; 11 12 import java.io.IOException; 13 import java.io.UnsupportedEncodingException; 14 import
java.net.URLEncoder; 15 import java.util.Map; 16 17 /** 18 * @author 19 * @description HTTP請求工具類 20 */ 21 public class HttpUtils { 22 23 /** 24 * 傳送GET請求 25 * 26 * @param url 請求URL 27 * @param queryParams 請求引數 28 * @return 響應結果 29 */ 30 public
static String doGet(String url, Map<String, Object> queryParams) 31 throws IOException { 32 // 拼接引數 33 if (queryParams != null && !queryParams.isEmpty()) { 34 url += "?" + urlEncode(queryParams); 35 } 36 37 // GET請求 38 HttpGet get = new HttpGet(url); 39 // 請求配置 40 get.setConfig(requestConfig()); 41 42 // 執行請求 43 return getResult(get); 44 } 45 46 /** 47 * 傳送POST請求 48 * 49 * @param url 請求URL 50 * @param bodyParams Body引數 51 * @return 響應結果 52 */ 53 public static String doPost(String url, Map<String, Object> bodyParams) 54 throws IOException { 55 // POST請求 56 HttpPost post = new HttpPost(url); 57 // 設定Body引數 58 setBodyParams(post, bodyParams); 59 // 請求配置 60 post.setConfig(requestConfig()); 61 62 // 執行請求 63 return getResult(post); 64 } 65 66 /** 67 * 傳送帶Header的GET請求 68 * 69 * @param url 請求URL 70 * @param headers 請求頭 71 * @param queryParams 查詢引數 72 * @return 響應結果 73 */ 74 public static String doGet(String url, Map<String, String> headers, Map<String, Object> queryParams) 75 throws IOException { 76 // 拼接引數 77 if (queryParams != null && !queryParams.isEmpty()) { 78 url += "?" + urlEncode(queryParams); 79 } 80 81 // GET請求 82 HttpGet get = new HttpGet(url); 83 // 設定請求頭 84 setHeaders(get, headers); 85 // 請求配置 86 get.setConfig(requestConfig()); 87 88 // 執行請求 89 return getResult(get); 90 } 91 92 /** 93 * 傳送帶Header的POST請求 94 * 95 * @param url 請求URL 96 * @param headers 請求頭 97 * @param bodyParams Body引數 98 * @return 響應結果 99 */ 100 public static String doPost(String url, Map<String, String> headers, Map<String, Object> bodyParams) 101 throws IOException { 102 // POST請求 103 HttpPost post = new HttpPost(url); 104 // 設定請求頭 105 setHeaders(post, headers); 106 // 設定Body引數 107 setBodyParams(post, bodyParams); 108 // 請求配置 109 post.setConfig(requestConfig()); 110 111 // 執行請求 112 return getResult(post); 113 } 114 115 /** 116 * 執行HTTP請求 117 * 118 * @param requestBase HTTP請求 119 * @return 響應結果 120 */ 121 private static String getResult(HttpRequestBase requestBase) 122 throws IOException { 123 // HTTP客戶端 124 CloseableHttpClient httpClient = HttpClients.createDefault(); 125 126 // 響應結果 127 String result = EntityUtils.toString(httpClient.execute(requestBase).getEntity()); 128 httpClient.close(); 129 130 // 返回響應結果 131 return result; 132 } 133 134 /** 135 * 請求配置 136 * 137 * @return 請求配置 138 */ 139 private static RequestConfig requestConfig() { 140 return RequestConfig 141 .custom() 142 // 連線主機超時時間 143 .setConnectTimeout(35000) 144 // 請求超時時間 145 .setConnectionRequestTimeout(35000) 146 // 資料讀取超時時間 147 .setSocketTimeout(60000) 148 .build(); 149 } 150 151 /** 152 * 設定請求頭 153 * 154 * @param requestBase HTTP請求 155 * @param headers 請求頭 156 */ 157 private static void setHeaders(HttpRequestBase requestBase, Map<String, String> headers) { 158 // 設定請求頭 159 if (headers != null && !headers.isEmpty()) { 160 for (Map.Entry<String, String> entry : headers.entrySet()) { 161 requestBase.setHeader(entry.getKey(), entry.getValue()); 162 } 163 } 164 } 165 166 /** 167 * URL引數編碼 168 * 169 * @param queryParams 請求引數 170 * @return 引數編碼結果 171 */ 172 private static String urlEncode(Map<?, ?> queryParams) 173 throws UnsupportedEncodingException { 174 StringBuilder sb = new StringBuilder(); 175 for (Map.Entry<?, ?> entry : queryParams.entrySet()) { 176 if (sb.length() > 0) { 177 sb.append("&"); 178 } 179 sb.append(String.format("%s=%s", 180 URLEncoder.encode(entry.getKey().toString(), "UTF-8"), 181 URLEncoder.encode(entry.getValue().toString(), "UTF-8") 182 )); 183 } 184 return sb.toString(); 185 } 186 187 /** 188 * 設定Body引數 189 * 190 * @param post POST請求 191 * @param bodyParams Body引數 192 */ 193 private static void setBodyParams(HttpPost post, Map<?, ?> bodyParams) 194 throws UnsupportedEncodingException { 195 HttpEntity entity = new StringEntity(JSONObject.toJSONString(bodyParams)); 196 post.setEntity(entity); 197 } 198 199 }