HTTP request encapsulation and proxy request explanation, request encapsulation proxy explanation
HTTP request encapsulation and proxy request details
Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. InputStreamReader;
Import java. io. OutputStream;
Import java. nio. charset. Charset;
Import java. util. List;
Import org. apache. commons. io. IOUtils;
Import org. apache. http. HttpEntity;
Import org. apache. http. HttpHost;
Import org. apache. http. NameValuePair;
Import org. apache. http. client. CookieStore;
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. protocol. HttpClientContext;
Import org. apache. http. entity. StringEntity;
Import org. apache. http. impl. client. BasicCookieStore;
Import org. apache. http. impl. client. CloseableHttpClient;
Import org. apache. http. impl. client. defaultonkeepalivestrategy;
Import org. apache. http. impl. client. DefaultRedirectStrategy;
Import org. apache. http. impl. client. HttpClientBuilder;
Import org. apache. http. impl. client. HttpClients;
Import org. apache. http. util. EntityUtils;
Import org. slf4j. Logger;
Import org. slf4j. LoggerFactory;
Public class HttpClientGetPostData implements IHttpGetPostData {
Private final static Logger log = LoggerFactory. getLogger (HttpClientGetPostData. class );
Private static IHttpGetPostData instance = null;
Private HttpClientGetPostData (){
}
Public static IHttpGetPostData getInstance (){
If (instance = null ){
Instance = new HttpClientGetPostData ();
}
Return instance;
}
@ Override
Public String getData (String url) throws IOException {
CloseableHttpClient httpclient = HttpClients. createSystem ();
HttpGet httpget = new HttpGet (url );
If (isUserProxy (url )){
// Proxy address, proxy port number, and protocol type
HttpHost proxy = new HttpHost (ResourceUtil. getProxyHostName (), ResourceUtil. getProxyPort (),
ResourceUtil. getProxyScheme ());
RequestConfig config = RequestConfig. custom (). setProxy (proxy). build ();
Httpget. setConfig (config );
}
Httpget. setHeader ("User-Agent ",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36 ");
// Httpget. getParams (). setParameter (CoreConnectionPNames. CONNECTION_TIMEOUT,
// 120*1000); // connection timeout
// Httpget. getParams (). setParameter (CoreConnectionPNames. SO_TIMEOUT,
// 120*1000); // read timeout
// Httpget. getParams (). setParameter (CoreProtocolPNames. HTTP_CONTENT_CHARSET, "UTF-8 ");
CloseableHttpResponse response = null;
Try {
Response = httpclient.exe cute (httpget );
// Httpclient. getParams (). setParameter (CoreProtocolPNames. HTTP_CONTENT_CHARSET,
// UTF-8 ");
HttpEntity entity = response. getEntity ();
Entity. getContentEncoding ();
BufferedReader br = new BufferedReader (new InputStreamReader (entity. getContent ()));
Int statusCode = response. getStatusLine (). getStatusCode ();
If (statusCode! = 200 ){
Log. warn ("post to" + url + ", return" + statusCode );
Return null;
}
If (entity! = Null ){
// Print the response Content Length
Log.info ("Response content length:" + entity. getContentLength ());
String ret = EntityUtils. toString (entity, "UTF-8 ");
// Print the response content
Log.info ("Response content:" + ret );
Return ret. toString ();
}
} Catch (Exception e ){
Httpget. abort ();
Log. error (e. toString ());
E. printStackTrace ();
} Finally {
If (response! = Null ){
Response. close ();
}
If (httpclient! = Null ){
Httpclient. close ();
}
}
Return null;
}
@ Override
Public Boolean savePic (String url, String filePath, String fileName) throws IOException {
CloseableHttpClient httpclient = HttpClients. createSystem ();
HttpGet httpget = new HttpGet (url );
If (isUserProxy (url )){
// Proxy address, proxy port number, and protocol type
HttpHost proxy = new HttpHost (ResourceUtil. getProxyHostName (), ResourceUtil. getProxyPort (),
ResourceUtil. getProxyScheme ());
RequestConfig config = RequestConfig. custom (). setProxy (proxy). build ();
Httpget. setConfig (config );
}
Httpget. setHeader ("User-Agent ",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36 ");
// Httpget. getParams (). setParameter (CoreConnectionPNames. CONNECTION_TIMEOUT,
// 120*1000); // connection timeout
// Httpget. getParams (). setParameter (CoreConnectionPNames. SO_TIMEOUT,
// 120*1000); // read timeout
// Httpget. getParams (). setParameter (CoreProtocolPNames. HTTP_CONTENT_CHARSET, "UTF-8 ");
CloseableHttpResponse response = null;
Try {
Response = httpclient.exe cute (httpget );
// Httpclient. getParams (). setParameter (CoreProtocolPNames. HTTP_CONTENT_CHARSET,
// UTF-8 ");
HttpEntity entity = response. getEntity ();
Entity. getContentEncoding ();
BufferedReader br = new BufferedReader (new InputStreamReader (entity. getContent ()));
InputStream input = entity. getContent ();
FileUtil. makedir (filePath );
OutputStream output = new FileOutputStream (new File (filePath + fileName ));
IOUtils. copy (input, output );
Output. flush ();
Int statusCode = response. getStatusLine (). getStatusCode ();
If (statusCode! = 200 ){
Log. warn ("getData from" + url + ", return" + statusCode );
Return false;
}
Return true;
} Catch (Exception e ){
Httpget. abort ();
Log. error (e. toString ());
E. printStackTrace ();
} Finally {
If (response! = Null ){
Response. close ();
}
If (httpclient! = Null ){
Httpclient. close ();
}
}
Return false;
}
@ Override
Public String postData (String url, List formParams) throws Exception {
CloseableHttpClient httpclient = HttpClients. createDefault ();
HttpPost httppost = new HttpPost (url );
If (isUserProxy (url )){
// Proxy address, proxy port number, and protocol type
HttpHost proxy = new HttpHost (ResourceUtil. getProxyHostName (), ResourceUtil. getProxyPort (),
ResourceUtil. getProxyScheme ());
RequestConfig config = RequestConfig. custom (). setProxy (proxy). build ();
Httppost. setConfig (config );
}
CloseableHttpResponse response = null;
Try {
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity (formParams, "UTF-8 ");
Httppost. setEntity (uefEntity );
Response = httpclient.exe cute (httppost );
HttpEntity entity = response. getEntity ();
Int statusCode = response. getStatusLine (). getStatusCode ();
If (statusCode! = 200 ){
Log. warn ("post to" + url + ", return" + statusCode + ", Data is" + formParams );
Return null;
}
If (entity! = Null ){
// Print the response Content Length
Log. debug ("Response content length:" + entity. getContentLength ());
String ret = EntityUtils. toString (entity, "UTF-8 ");
// Print the response content
Log. debug ("Response content:" + ret );
Return ret;
}
} Catch (Exception e ){
Httppost. abort ();
Log. error (e. toString ());
E. printStackTrace ();
} Finally {
If (response! = Null ){
Response. close ();
}
If (httpclient! = Null ){
Httpclient. close ();
}
}
Return url;
}
@ Override
Public String postJsonData (String url, String jsonstr) throws Exception {
Log.info (jsonstr );
CloseableHttpClient httpclient = HttpClients. createDefault ();
HttpPost httppost = new HttpPost (url );
If (isUserProxy (url )){
// Proxy address, proxy port number, and protocol type
HttpHost proxy = new HttpHost (ResourceUtil. getProxyHostName (), ResourceUtil. getProxyPort (),
ResourceUtil. getProxyScheme ());
RequestConfig config = RequestConfig. custom (). setProxy (proxy). build ();
Httppost. setConfig (config );
}
CloseableHttpResponse response = null;
Try {
StringEntity s = new StringEntity (jsonstr, Charset. forName ("UTF-8 "));
S. setContentEncoding ("UTF-8 ");
S. setContentType ("application/json ");
Httppost. setEntity (s );
Httppost. addHeader ("Content-type", "application/json ");
Httppost. setHeader ("Accept", "application/json ");
Response = httpclient.exe cute (httppost );
HttpEntity entity = response. getEntity ();
Int statusCode = response. getStatusLine (). getStatusCode ();
If (statusCode! = 200 ){
Log. warn ("post to" + url + ", return" + statusCode + ", Data is" + jsonstr );
Return null;
}
If (entity! = Null ){
// Print the response Content Length
Log. debug ("Response content length:" + entity. getContentLength ());
String ret = EntityUtils. toString (entity, "UTF-8 ");
// Print the response content
Log. debug ("Response content:" + ret );
Return ret;
}
} Catch (Exception e ){
Httppost. abort ();
Log. error (e. toString ());
E. printStackTrace ();
} Finally {
If (response! = Null ){
Response. close ();
}
If (httpclient! = Null ){
Httpclient. close ();
}
}
Return url;
}
Public static CloseableHttpClient httpClientKeepSession = null;
Public static HttpClientContext context = null;
Public static CookieStore cookieStore = null;
Public static RequestConfig requestConfig = null;
Static {
Init ();
}
Private static void init (){
Context = HttpClientContext. create ();
CookieStore = new BasicCookieStore ();
// Configure the timeout time (1 second for connection to the server and 2 seconds for response of request data)
RequestConfig = RequestConfig. custom (). setConnectTimeout (120*1000). setSocketTimeout (60*1000)
. SetConnectionRequestTimeout (60*1000). build ();
// Set the default redirect and cookie Storage
HttpClientKeepSession = HttpClientBuilder. create ()
. SetKeepAliveStrategy (new defaultonkeepalivestrategy ())
. SetRedirectStrategy (new DefaultRedirectStrategy (). setDefaultRequestConfig (requestConfig)
. Setdefacookcookiestore (cookieStore). build ();
}
@ Override
Public String postJsonDataKeepSession (String url, String jsonstr) throws Exception {
Log.info ("request address:" + url );
Log.info ("request parameter:" + jsonstr );
HttpPost httppost = new HttpPost (url );
If (isUserProxy (url )){
// Proxy address, proxy port number, and protocol type
HttpHost proxy = new HttpHost (ResourceUtil. getProxyHostName (), ResourceUtil. getProxyPort (),
ResourceUtil. getProxyScheme ());
RequestConfig config = RequestConfig. custom (). setProxy (proxy). build ();
Httppost. setConfig (config );
}
CloseableHttpResponse response = null;
Try {
StringEntity s = new StringEntity (jsonstr, Charset. forName ("UTF-8 "));
S. setContentEncoding ("UTF-8 ");
S. setContentType ("application/json ");
Httppost. setEntity (s );
Httppost. addHeader ("Content-type", "application/json ");
Httppost. setHeader ("Accept", "application/json ");
Response = httpClientKeepSession.exe cute (httppost );
HttpEntity entity = response. getEntity ();
Int statusCode = response. getStatusLine (). getStatusCode ();
If (statusCode! = 200 ){
Log. warn ("post to" + url + ", return" + statusCode + ", Data is" + jsonstr );
Return null;
}
If (entity! = Null ){
// Print the response Content Length
Log. debug ("Response content length:" + entity. getContentLength ());
String ret = EntityUtils. toString (entity, "UTF-8 ");
// Print the response content
Log. debug ("Response content:" + ret );
Return ret;
}
} Catch (Exception e ){
Httppost. abort ();
Log. error (e. toString ());
E. printStackTrace ();
} Finally {
If (response! = Null ){
Response. close ();
}
}
Return url;
}
@ Override
Public String getDataKeepSession (String url) throws IOException {
HttpGet httpget = new HttpGet (url );
If (isUserProxy (url )){
// Proxy address, proxy port number, and protocol type
HttpHost proxy = new HttpHost (ResourceUtil. getProxyHostName (), ResourceUtil. getProxyPort (),
ResourceUtil. getProxyScheme ());
RequestConfig config = RequestConfig. custom (). setProxy (proxy). build ();
Httpget. setConfig (config );
}
Httpget. setHeader ("User-Agent ",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36 ");
// Httpget. getParams (). setParameter (CoreConnectionPNames. CONNECTION_TIMEOUT,
// 120*1000); // connection timeout
// Httpget. getParams (). setParameter (CoreConnectionPNames. SO_TIMEOUT,
// 120*1000); // read timeout
// Httpget. getParams (). setParameter (CoreProtocolPNames. HTTP_CONTENT_CHARSET, "UTF-8 ");
CloseableHttpResponse response = null;
Try {
Response = httpClientKeepSession.exe cute (httpget );
// Httpclient. getParams (). setParameter (CoreProtocolPNames. HTTP_CONTENT_CHARSET,
// UTF-8 ");
HttpEntity entity = response. getEntity ();
Entity. getContentEncoding ();
BufferedReader br = new BufferedReader (new InputStreamReader (entity. getContent ()));
Int statusCode = response. getStatusLine (). getStatusCode ();
If (statusCode! = 200 ){
Log. warn ("post to" + url + ", return" + statusCode );
Return null;
}
If (entity! = Null ){
// Print the response Content Length
Log.info ("Response content length:" + entity. getContentLength ());
String ret = EntityUtils. toString (entity, "UTF-8 ");
// Print the response content
Log.info ("Response content:" + ret );
Return ret. toString ();
}
} Catch (Exception e ){
Httpget. abort ();
Log. error (e. toString ());
E. printStackTrace ();
} Finally {
If (response! = Null ){
Response. close ();
}
}
Return null;
}
/**
* Determines whether a proxy is used. If the proxy is an intranet address, no proxy is required. Otherwise, the proxy is determined based on the configuration.
*
* @ Param url
* @ Return
*/
Private Boolean isUserProxy (String url ){
// If it is an intranet address, no proxy is required.
String [] lanprefixs = ResourceUtil. getLanPrefix (). split (",");
For (String lanprefix: lanprefixs ){
If (url. startsWith (lanprefix )){
Return false;
}
}
Return ResourceUtil. getUseProxy ();
}
}