1、HttpClient 簡介java
HttpClient 是 Apache Jakarta Common 下的子項目,用來提供高效的、最新的、功能豐富的支持 HTTP 協議的客戶端編程工具包,而且它支持 HTTP 協議最新的版本和建議。Java後臺使用httpclient主要目的是爲了模擬客戶端的請求。apache
實現了全部的Http請求類型,相應的類爲:HttpGet、HttpPost、HttpDelete、HttpPut編程
1)導包json
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.5</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.5</version> </dependency>
2)建立鏈接所須要的配置信息安全
public class HttpClientConfig { static int httpConnectTimeout = 10000;//鏈接超時時間(單位毫秒) static int httpSocketTimeout = 10000;//socket讀寫超時時間(單位毫秒) static int httpMaxPoolSize = 100; static int httpMonitorInterval = 3000; static int httpIdelTimeout = 2000; public static int getHttpIdelTimeout() { return httpIdelTimeout; } public static int getHttpSocketTimeout() { return httpSocketTimeout; } public static int getHttpMaxPoolSize() { return httpMaxPoolSize; } public static int getHttpMonitorInterval() { return httpMonitorInterval; } public static int getHttpConnectTimeout() { return httpConnectTimeout; } }
3)封裝HttpClientUtils類--包括鏈接池的信息服務器
public class HttpClientUtils { private final static Logger logger = Logger.getLogger(HttpClientUtils.class); private static CloseableHttpClient httpClient; private static PoolingHttpClientConnectionManager manager; // 鏈接池管理類 private static ScheduledExecutorService monitorExecutor; // 監控 private final static Object syncLock = new Object(); // 至關於線程鎖,用於線程安全 private static final int CONNECT_TIMEOUT = HttpClientConfig.getHttpConnectTimeout();// 設置鏈接創建的超時時間爲10s private static final int SOCKET_TIMEOUT = HttpClientConfig.getHttpSocketTimeout(); private static final int MAX_CONN = HttpClientConfig.getHttpMaxPoolSize(); // 最大鏈接數 private static final int Max_PRE_ROUTE = HttpClientConfig.getHttpMaxPoolSize(); private static final int MAX_ROUTE = HttpClientConfig.getHttpMaxPoolSize(); /** * 對http請求進行基本設置 * * @param httpRequestBase * http請求 */ private static void setRequestConfig(HttpRequestBase httpRequestBase) { RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(CONNECT_TIMEOUT) .setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build(); httpRequestBase.setConfig(requestConfig); } public static CloseableHttpClient getHttpClient(String url) { String hostName = url.split("/")[2]; // System.out.println(hostName); int port = 80; if (hostName.contains(":")) { String[] args = hostName.split(":"); hostName = args[0]; port = Integer.parseInt(args[1]); } if (httpClient == null) { // 多線程下多個線程同時調用getHttpClient容易致使重複建立httpClient對象的問題,因此加上了同步鎖 synchronized (syncLock) { if (httpClient == null) { httpClient = createHttpClient(hostName, port); // 開啓監控線程,對異常和空閒線程進行關閉 monitorExecutor = Executors.newScheduledThreadPool(1); monitorExecutor.scheduleAtFixedRate(new TimerTask() { @Override public void run() { // 關閉異常鏈接 manager.closeExpiredConnections(); // 關閉5s空閒的鏈接 manager.closeIdleConnections(HttpClientConfig.getHttpIdelTimeout(), TimeUnit.MILLISECONDS); logger.debug("close expired and idle for over 5s connection"); } }, HttpClientConfig.getHttpMonitorInterval(), HttpClientConfig.getHttpMonitorInterval(), TimeUnit.MILLISECONDS); } } } return httpClient; } /** * 根據host和port構建httpclient實例 * * @param host * 要訪問的域名 * @param port * 要訪問的端口 * @return */ public static CloseableHttpClient createHttpClient(String host, int port) { ConnectionSocketFactory plainSocketFactory = PlainConnectionSocketFactory.getSocketFactory(); LayeredConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactory.getSocketFactory(); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create() .register("http", plainSocketFactory).register("https", sslSocketFactory).build(); manager = new PoolingHttpClientConnectionManager(registry); // 設置鏈接參數 manager.setMaxTotal(MAX_CONN); // 最大鏈接數 manager.setDefaultMaxPerRoute(Max_PRE_ROUTE); // 路由最大鏈接數 HttpHost httpHost = new HttpHost(host, port); manager.setMaxPerRoute(new HttpRoute(httpHost), MAX_ROUTE); // 請求失敗時,進行請求重試 HttpRequestRetryHandler handler = new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException e, int i, HttpContext httpContext) { if (i > 3) { // 重試超過3次,放棄請求 logger.error("retry has more than 3 time, give up request"); return false; } if (e instanceof NoHttpResponseException) { // 服務器沒有響應,多是服務器斷開了鏈接,應該重試 logger.error("receive no response from server, retry"); return true; } if (e instanceof SSLHandshakeException) { // SSL握手異常 logger.error("SSL hand shake exception"); return false; } if (e instanceof InterruptedIOException) { // 超時 logger.error("InterruptedIOException"); return false; } if (e instanceof UnknownHostException) { // 服務器不可達 logger.error("server host unknown"); return false; } if (e instanceof ConnectTimeoutException) { // 鏈接超時 logger.error("Connection Time out"); return false; } if (e instanceof SSLException) { logger.error("SSLException"); return false; } HttpClientContext context = HttpClientContext.adapt(httpContext); HttpRequest request = context.getRequest(); if (!(request instanceof HttpEntityEnclosingRequest)) { // 若是請求不是關閉鏈接的請求 return true; } return false; } }; CloseableHttpClient client = HttpClients.custom().setConnectionManager(manager).setRetryHandler(handler) .build(); return client; } /** * 關閉鏈接池 */ public static void closeConnectionPool() { try { httpClient.close(); manager.close(); monitorExecutor.shutdown(); } catch (IOException e) { e.printStackTrace(); } } }
/** * 對http請求進行基本設置 * * @param httpRequestBase * http請求 */ private static void setRequestConfig(HttpRequestBase httpRequestBase) { RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(CONNECT_TIMEOUT) .setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build(); httpRequestBase.setConfig(requestConfig); }
4)form表單提交微信
public static String doPostForm(String url, Map<String, String> params) { HttpPost httpPost = new HttpPost(url); setRequestConfig(httpPost); String resultString = ""; CloseableHttpResponse response = null; try { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); if (params != null) { for (String key : params.keySet()) { builder.addPart(key, new StringBody(params.get(key), ContentType.create("text/plain", Consts.UTF_8))); } } HttpEntity reqEntity = builder.build(); httpPost.setEntity(reqEntity); // 發起請求 並返回請求的響應 response = getHttpClient(url).execute(httpPost, HttpClientContext.create()); resultString = EntityUtils.toString(response.getEntity(), "utf-8"); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (response != null) response.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString; }
5)File文件上傳多線程
public static String uploadFile(String url, String localFile, String fileParamName, Map<String, String> params) { HttpPost httpPost = new HttpPost(url); setRequestConfig(httpPost); String resultString = ""; CloseableHttpResponse response = null; try { // 把文件轉換成流對象FileBody FileBody bin = new FileBody(new File(localFile)); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); // 至關於<input type="file" name="file"/> builder.addPart("files", bin); // 至關於<input type="text" name="userName" value=userName> builder.addPart("filesFileName", new StringBody(fileParamName, ContentType.create("text/plain", Consts.UTF_8))); if (params != null) { for (String key : params.keySet()) { builder.addPart(key, new StringBody(params.get(key), ContentType.create("text/plain", Consts.UTF_8))); } } HttpEntity reqEntity = builder.build(); httpPost.setEntity(reqEntity); // 發起請求 並返回請求的響應 response = getHttpClient(url).execute(httpPost, HttpClientContext.create()); resultString = EntityUtils.toString(response.getEntity(), "utf-8"); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (response != null) response.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString; }
6) 傳輸Json數據app
public static String doPostJson(String url, String json) { HttpPost httpPost = new HttpPost(url); setRequestConfig(httpPost); String resultString = ""; CloseableHttpResponse response = null; try { // 設置ContentType(注:若是隻是傳普通參數的話,ContentType不必定非要用application/json) // httpPost.setHeader("Content-Type", // "application/json;charset=utf8"); httpPost.setHeader("Content-Type", "application/json"); // 建立請求內容 StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); httpPost.setEntity(entity); // 執行http請求 response = getHttpClient(url).execute(httpPost, HttpClientContext.create()); resultString = EntityUtils.toString(response.getEntity(), "utf-8"); } catch (Exception e) { logger.error("httpclient的get請求失敗,url:" + url, e); // e.printStackTrace(); } finally { try { if (response != null) response.close(); } catch (IOException e) { logger.error("IOException的錯誤", e); // e.printStackTrace(); } } return resultString; }
歡迎關注微信公衆號【Java典籍】,收看更多Java技術乾貨!關注即送java全套資料一份socket
▼微信掃一掃下圖↓↓↓二維碼關注