一、HttpClient 簡介
HttpClient 是 Apache Jakarta Common 下的子項目,用來提供高效的、最新的、功能豐富的支持 HTTP 協議的客戶端編程工具包,並且它支持 HTTP 協議最新的版本和建議。Java后台使用httpclient主要目的是為了模擬客戶端的請求。
2、HttpClient的請求類型
實現了所有的Http請求類型,相應的類為:HttpGet、HttpPost、HttpDelete、HttpPut
3、Http的使用流程
1)導包
<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類--包括連接池的信息
/**
*導包
*/
import com.scxd.config.HttpClientConfig; import org.apache.http.*; import org.apache.http.client.HttpRequestRetryHandler; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.LayeredConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; import javax.net.ssl.SSLException; import javax.net.ssl.SSLHandshakeException; import java.io.IOException; import java.io.InterruptedIOException; import java.net.UnknownHostException; import java.util.Map; import java.util.TimerTask; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import org.apache.log4j.Logger;
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數據
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; }