Preface
Recently, in the library that encapsulates the HTTP request, I want the interface's behavior and implementation to be separated so that the underlying library switches to different HTTP request implementations such as Apache HttpClient, OkHttp, asynchttpclient does not affect the upper-level invocation behavior.
About Apache HttpClient, OkHttp, asynchttpclient I will introduce each, this article mainly on the Apache HttpClient to do the introduction. Tutorial
HttpClient 4.5 Tutorial is mainly divided into: basic (Fundamentals), connection management (Connection Management), HTTP status Management (HTTP state management), 7 topics such as HTTP authorization (HTTP authentication), streaming API (Fluent API), HTTP caching (HTTP Caching), Advanced topics (topics).
Before using HttpClient 4.5, it is advisable to have a detailed understanding of the above 7 topics in order to better use & tuning in development. Introduction
HttpClient is the Apache official launch of an excellent HTTP request client library.
HttpClient features Client-side HTTP Transport library based on Httpcore based on classic (blocking) I/O Content agnostic Best Practice
Maven Dependency
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId> httpclient</artifactid>
<version>4.5.2</version>
</dependency>
Apachehttpclientimpl class
Package Com.bytebeats.toolkit.http.impl;
Import Com.bytebeats.toolkit.annotation.ThreadSafe;
Import com.bytebeats.toolkit.http.HttpRequestException;
Import Com.bytebeats.toolkit.http.config.FormParam;
Import Com.bytebeats.toolkit.http.config.HttpRequestConfig;
Import org.apache.http.*;
Import org.apache.http.client.ClientProtocolException;
Import Org.apache.http.client.config.RequestConfig;
Import org.apache.http.client.entity.UrlEncodedFormEntity;
Import Org.apache.http.client.methods.CloseableHttpResponse;
Import Org.apache.http.client.methods.HttpGet;
Import Org.apache.http.client.methods.HttpPost;
Import Org.apache.http.client.methods.HttpUriRequest;
Import Org.apache.http.client.protocol.HttpClientContext;
Import Org.apache.http.entity.ContentType;
Import org.apache.http.entity.StringEntity;
Import org.apache.http.impl.client.CloseableHttpClient;
Import Org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
Import org.apache.http.impl.client.HttpClients; Import org.apache.htTp.impl.conn.PoolingHttpClientConnectionManager;
Import Org.apache.http.message.BasicNameValuePair;
Import Org.apache.http.protocol.HttpContext;
Import Org.apache.http.util.EntityUtils;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import java.io.IOException;
Import java.util.ArrayList;
Import java.util.List; /** * https://hc.apache.org/httpcomponents-client-4.5.x/quickstart.html * https://hc.apache.org/ httpcomponents-client-4.5.x/tutorial/html/connmgmt.html * * @author Ricky Fung * @create 2016-08-20 18:00 * * @ThreadSa Fe public class Apachehttpclientimpl extends HttpClient {private final Logger Logger = Loggerfactory.getlogger (Apach
Ehttpclientimpl.class); Private final threadlocal
Httprequestconfig class
Package com.bytebeats.toolkit.http.config; /** * ${description} * * @author Ricky Fung * @create 2016-08-24 19:06 * public class Httprequestconfig {Publi
C static final Httprequestconfig DEFAULT = new Httprequestconfig ();
private int readtimeout = 6000;
private int sockettimeout = 6000;
private int connecttimeout = 6000;
private int connectionrequesttimeout = 6000;
private int maxconnections = 200;
private int maxconnectionperhost = 50;
Private Boolean requestretryenabled;
Private Boolean RetryCount;
Proxy server private Httphost proxy;
public int getreadtimeout () {return readtimeout;
} public void Setreadtimeout (int readtimeout) {this.readtimeout = ReadTimeout;
} public int Getsockettimeout () {return sockettimeout;
} public void SetSocketTimeout (int sockettimeout) {this.sockettimeout = Sockettimeout; } public int Getconnecttimeout () {return ConnecttiMeout;
} public void Setconnecttimeout (int connecttimeout) {this.connecttimeout = ConnectTimeout;
} public int Getconnectionrequesttimeout () {return connectionrequesttimeout; } public void Setconnectionrequesttimeout (int connectionrequesttimeout) {this.connectionrequesttimeout = con
Nectionrequesttimeout;
} public int Getmaxconnectionperhost () {return maxconnectionperhost; } public void Setmaxconnectionperhost (int maxconnectionperhost) {this.maxconnectionperhost = Maxconnectionpe
RHost;
} public boolean isrequestretryenabled () {return requestretryenabled; } public void Setrequestretryenabled (Boolean requestretryenabled) {this.requestretryenabled = Requestretryen
abled;
} public boolean Isretrycount () {return retrycount;
The public void Setretrycount (Boolean retrycount) {this.retrycount = RetryCount; } public int GetmaxconnEctions () {return maxconnections;
} public void setmaxconnections (int maxconnections) {this.maxconnections = MaxConnections;
} public Httphost GetProxy () {return proxy;
} public void SetProxy (Httphost proxy) {this.proxy = proxy;
}
}
Formparam class
Package com.bytebeats.toolkit.http.config;
/**
* ${description}
*
* @author Ricky Fung
* @create 2016-08-24 16:17
*/public class Formparam {
private final String name;
Private final String value;
Public Formparam (string name, String value) {
this.name = name;
This.value = value;
}
Public String GetName () {
return this.name;
}
Public String GetValue () {
return this.value;
}
}
References
Fundamentals
Connection Management
HTTP State Management
HTTP Authentication
Fluent API
HTTP Caching
Advanced Topics Reference
HttpClient 4.5 tutorial:https://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/index.html