欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

HTTP请求封装以及代理请求详解

程序员文章站 2022-09-19 18:33:33
HTTP请求封装以及代理请求详解 import java.io.BufferedReader; import java.io.File; import java.io.Fil...

HTTP请求封装以及代理请求详解

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.DefaultConnectionKeepAliveStrategy;

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)) {

// 依次是代理地址,代理端口号,协议类型

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);//连接超时

// httpget.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,

// 120*1000);//读取超时

// httpget.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,"UTF-8");

CloseableHttpResponse response = null;

try {

response = httpclient.execute(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) {

// 打印响应内容长度

log.info("Response content length: " + entity.getContentLength());

String ret = EntityUtils.toString(entity, "UTF-8");

// 打印响应内容

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)) {

// 依次是代理地址,代理端口号,协议类型

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);//连接超时

// httpget.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,

// 120*1000);//读取超时

// httpget.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,"UTF-8");

CloseableHttpResponse response = null;

try {

response = httpclient.execute(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)) {

// 依次是代理地址,代理端口号,协议类型

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.execute(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) {

// 打印响应内容长度

log.debug("Response content length: " + entity.getContentLength());

String ret = EntityUtils.toString(entity, "UTF-8");

// 打印响应内容

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)) {

// 依次是代理地址,代理端口号,协议类型

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.execute(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) {

// 打印响应内容长度

log.debug("Response content length: " + entity.getContentLength());

String ret = EntityUtils.toString(entity, "UTF-8");

// 打印响应内容

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();

// 配置超时时间(连接服务端超时1秒,请求数据返回超时2秒)

requestConfig = RequestConfig.custom().setConnectTimeout(120*1000).setSocketTimeout(60*1000)

.setConnectionRequestTimeout(60*1000).build();

// 设置默认跳转以及存储cookie

httpClientKeepSession = HttpClientBuilder.create()

.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())

.setRedirectStrategy(new DefaultRedirectStrategy()).setDefaultRequestConfig(requestConfig)

.setDefaultCookieStore(cookieStore).build();

}

@Override

public String postJsonDataKeepSession(String url, String jsonstr) throws Exception {

log.info("请求地址:"+url);

log.info("请求参数:"+jsonstr);

HttpPost httppost = new HttpPost(url);

if (isUserProxy(url)) {

// 依次是代理地址,代理端口号,协议类型

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.execute(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) {

// 打印响应内容长度

log.debug("Response content length: " + entity.getContentLength());

String ret = EntityUtils.toString(entity, "UTF-8");

// 打印响应内容

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)) {

// 依次是代理地址,代理端口号,协议类型

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);//连接超时

// httpget.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,

// 120*1000);//读取超时

// httpget.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,"UTF-8");

CloseableHttpResponse response = null;

try {

response = httpClientKeepSession.execute(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) {

// 打印响应内容长度

log.info("Response content length: " + entity.getContentLength());

String ret = EntityUtils.toString(entity, "UTF-8");

// 打印响应内容

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;

}

/**

* 判断是否用代理 如果是内网地址,则不用代理 否则根据配置判断

*

* @param url

* @return

*/

private Boolean isUserProxy(String url) {

// 如果是内网地址,则不用代理

String[] lanprefixs=ResourceUtil.getLanPrefix().split(",");

for(String lanprefix:lanprefixs) {

if (url.startsWith(lanprefix)) {

return false;

}

}

return ResourceUtil.getUseProxy();

}

}