這個是httpClient的get請求寫法,一般需求應該是夠用了。
參考了幾個博客的代碼,具體的網址忘記了,有問題可以聯系我。
代碼:
1 package com.demo.service.impl; 2 3 import com.demo.service.HttpRequestGetService; 4 import com.demo.utils.CloseHttpUtil; 5 import org.apache.http.HttpEntity; 6 import org.apache.http.ParseException; 7 import org.apache.http.client.ClientProtocolException; 8 import org.apache.http.client.methods.CloseableHttpResponse; 9 import org.apache.http.client.methods.HttpGet; 10 import org.apache.http.client.utils.URIBuilder; 11 import org.apache.http.impl.client.CloseableHttpClient; 12 import org.apache.http.impl.client.HttpClients; 13 import org.apache.http.util.EntityUtils; 14 import org.springframework.stereotype.Component; 15 16 import java.io.IOException; 17 import java.net.URISyntaxException; 18 import java.util.Map; 19 20 @Component 21 public class HttpRequestGetServiceImpl implements HttpRequestGetService { 22 CloseHttpUtil closeHttpUtil = new CloseHttpUtil(); 23 24 public String doGetRequest(String url, Map<String, String> headMap, Map<String, String> paramMap) { 25 // 獲取連接客戶端工具 26 CloseableHttpClient httpClient = HttpClients.createDefault(); 27 String entityStr = null; 28 CloseableHttpResponse response = null; 29 try { 30 /* 31 * 由於GET請求的參數都是拼裝在URL地址后方,所以我們要構建一個URL,帶參數 32 */ 33 URIBuilder uriBuilder = new URIBuilder(url); 34 for (Map.Entry<String, String> param : paramMap.entrySet()) { 35 uriBuilder.addParameter(param.getKey(), param.getValue()); 36 } 37 // 根據帶參數的URI對象構建GET請求對象 38 HttpGet httpGet = new HttpGet(uriBuilder.build()); 39 40 /* 41 * 添加請求頭信息 42 */ 43 for (Map.Entry<String, String> header : headMap.entrySet()) { 44 httpGet.addHeader(header.getKey(), header.getValue()); 45 } 46 /*httpGet.addHeader("Content-Type", "application/VIID+JSON;charset=utf8"); 47 httpGet.addHeader("User-Identify","12345678905030000000");*/ 48 closeHttpUtil.setTimeOut(httpGet); 49 // 執行請求 50 response = httpClient.execute(httpGet); 51 // 獲得響應的實體對象 52 HttpEntity entity = response.getEntity(); 53 // 使用Apache提供的工具類進行轉換成字符串 54 entityStr = EntityUtils.toString(entity, "UTF-8"); 55 } catch (ClientProtocolException e) { 56 System.err.println("Http協議出現問題"); 57 e.printStackTrace(); 58 } catch (ParseException e) { 59 System.err.println("解析錯誤"); 60 e.printStackTrace(); 61 } catch (URISyntaxException e) { 62 System.err.println("URI解析異常"); 63 e.printStackTrace(); 64 } catch (IOException e) { 65 System.err.println("IO異常"); 66 e.printStackTrace(); 67 } finally { 68 // 釋放連接 69 closeHttpUtil.close(response, httpClient); 70 } 71 72 return entityStr; 73 } 74 75 public String doGetRequestNoparam(String url, Map<String, String> headMap) { 76 // 獲取連接客戶端工具 77 CloseableHttpClient httpClient = HttpClients.createDefault(); 78 String entityStr = null; 79 CloseableHttpResponse response = null; 80 try { 81 /* 82 * 由於GET請求的參數都是拼裝在URL地址后方,所以我們要構建一個URL,帶參數 83 */ 84 URIBuilder uriBuilder = new URIBuilder(url); 85 // 根據帶參數的URI對象構建GET請求對象 86 HttpGet httpGet = new HttpGet(uriBuilder.build()); 87 /* 88 * 添加請求頭信息 89 */ 90 for (Map.Entry<String, String> header : headMap.entrySet()) { 91 httpGet.addHeader(header.getKey(), header.getValue()); 92 } 93 /*httpGet.addHeader("Content-Type", "application/VIID+JSON;charset=utf8"); 94 httpGet.addHeader("User-Identify","12345678905030000000");*/ 95 closeHttpUtil.setTimeOut(httpGet); 96 // 執行請求 97 response = httpClient.execute(httpGet); 98 // 獲得響應的實體對象 99 HttpEntity entity = response.getEntity(); 100 // 使用Apache提供的工具類進行轉換成字符串 101 entityStr = EntityUtils.toString(entity, "UTF-8"); 102 } catch (ClientProtocolException e) { 103 System.err.println("Http協議出現問題"); 104 e.printStackTrace(); 105 } catch (ParseException e) { 106 System.err.println("解析錯誤"); 107 e.printStackTrace(); 108 } catch (URISyntaxException e) { 109 System.err.println("URI解析異常"); 110 e.printStackTrace(); 111 } catch (IOException e) { 112 System.err.println("IO異常"); 113 e.printStackTrace(); 114 } finally { 115 // 釋放連接 116 closeHttpUtil.close(response, httpClient); 117 } 118 return entityStr; 119 } 120 121 public String doGetRequestStringParam(String url, Map<String, String> headMap, String param) { 122 // 獲取連接客戶端工具 123 CloseableHttpClient httpClient = HttpClients.createDefault(); 124 String entityStr = null; 125 CloseableHttpResponse response = null; 126 try { 127 /* 128 * 由於GET請求的參數都是拼裝在URL地址后方,所以我們要構建一個URL,帶參數 129 */ 130 URIBuilder uriBuilder = new URIBuilder(url); 131 // 根據帶參數的URI對象構建GET請求對象 132 HttpGet httpGet = new HttpGet(uriBuilder.build() + param); 133 System.out.println("調用String參數的URL:" + httpGet); 134 /* 135 * 添加請求頭信息 136 */ 137 for (Map.Entry<String, String> header : headMap.entrySet()) { 138 httpGet.addHeader(header.getKey(), header.getValue()); 139 } 140 closeHttpUtil.setTimeOut(httpGet); 141 response = httpClient.execute(httpGet); 142 // 獲得響應的實體對象 143 HttpEntity entity = response.getEntity(); 144 // 使用Apache提供的工具類進行轉換成字符串 145 entityStr = EntityUtils.toString(entity, "UTF-8"); 146 } catch (ClientProtocolException e) { 147 System.err.println("Http協議出現問題"); 148 e.printStackTrace(); 149 } catch (ParseException e) { 150 System.err.println("解析錯誤"); 151 e.printStackTrace(); 152 } catch (URISyntaxException e) { 153 System.err.println("URI解析異常"); 154 e.printStackTrace(); 155 } catch (IOException e) { 156 System.err.println("IO異常"); 157 e.printStackTrace(); 158 } finally { 159 // 釋放連接 160 closeHttpUtil.close(response, httpClient); 161 162 } 163 return entityStr; 164 } 165 166 public String doGetRequestIntParam(String url, Map<String, String> headMap, int RecordStartNo, int PageRecordNum) { 167 // 獲取連接客戶端工具 168 CloseableHttpClient httpClient = HttpClients.createDefault(); 169 String entityStr = null; 170 CloseableHttpResponse response = null; 171 try { 172 /* 173 * 由於GET請求的參數都是拼裝在URL地址后方,所以我們要構建一個URL,帶參數 174 */ 175 URIBuilder uriBuilder = new URIBuilder(url); 176 // 根據帶參數的URI對象構建GET請求對象 177 HttpGet httpGet = new HttpGet(uriBuilder.build() + "?" + "RecordStartNo=" + RecordStartNo + "&" + "PageRecordNum=" + PageRecordNum); 178 System.out.println("調用String參數的URL:" + httpGet); 179 /* 180 * 添加請求頭信息 181 */ 182 for (Map.Entry<String, String> header : headMap.entrySet()) { 183 httpGet.addHeader(header.getKey(), header.getValue()); 184 } 185 closeHttpUtil.setTimeOut(httpGet); 186 response = httpClient.execute(httpGet); 187 // 獲得響應的實體對象 188 HttpEntity entity = response.getEntity(); 189 // 使用Apache提供的工具類進行轉換成字符串 190 entityStr = EntityUtils.toString(entity, "UTF-8"); 191 } catch (ClientProtocolException e) { 192 System.err.println("Http協議出現問題"); 193 e.printStackTrace(); 194 } catch (ParseException e) { 195 System.err.println("解析錯誤"); 196 e.printStackTrace(); 197 } catch (URISyntaxException e) { 198 System.err.println("URI解析異常"); 199 e.printStackTrace(); 200 } catch (IOException e) { 201 System.err.println("IO異常"); 202 e.printStackTrace(); 203 } finally { 204 // 釋放連接 205 closeHttpUtil.close(response, httpClient); 206 } 207 return entityStr; 208 } 209 }
__________________________________________________________________
1 import org.apache.http.client.config.RequestConfig; 2 import org.apache.http.client.methods.CloseableHttpResponse; 3 import org.apache.http.client.methods.HttpGet; 4 import org.apache.http.client.methods.HttpPut; 5 import org.apache.http.impl.client.CloseableHttpClient; 6 7 import java.io.IOException; 8 9 public class CloseHttpUtil { 10 public static void close(CloseableHttpResponse response, CloseableHttpClient httpClient) { 11 if (null != response) { 12 try { 13 response.close(); 14 httpClient.close(); 15 } catch (IOException e) { 16 System.err.println("釋放連接出錯"); 17 e.printStackTrace(); 18 } 19 } 20 } 21 22 public static void setTimeOut(HttpGet httpGet) { 23 RequestConfig requestConfig = RequestConfig.custom() 24 // 設置連接超時時間(單位毫秒) 25 .setConnectTimeout(10000) 26 // 設置請求超時時間(單位毫秒) 27 .setConnectionRequestTimeout(10000) 28 // socket讀寫超時時間(單位毫秒) 29 .setSocketTimeout(10000) 30 // 設置是否允許重定向(默認為true) 31 .setRedirectsEnabled(true).build(); 32 33 // 將上面的配置信息 運用到這個Get請求里 34 httpGet.setConfig(requestConfig); 35 }