封装Httpclient的原因是,我觉得Build Httpclient都是一个麻烦事儿,所以提供了一个HttpClientBuilder,希望再build的时候,自动把两个组件植入进去。然而发现InnerHttpClient不让我在外面创建,怒了之后把InnerHttpclient的代码给扒出来了。然后我发现可以做的事情很多了,比如默认的User-agent给替换成一个真实浏览器,而不是HttpClient,比如封装常见的访问接口。可以直接传入一个url拿到数据等等。
需要注意的是HttpInvoker定制了cookieStore,目的是不允许服务器下发的cookie过期时间太久,目前做法是不能超过当前时间之内的一个小时,超过后设置为一个小时。对于HttpInvoker,他是一个静态工具类,所以不能提供多个用户同时访问一个站点的支持,因为任何时刻,他们都会使用同一个cookie空间。这样也不适合需要保持session的场景(单个用户单次业务操作在一个小时之类还是可以)。改造cookieStore的原因是同一个httpclient在使用时间太久之后,会收到服务器下发的各种cookie,也就导致无用cookie过度膨胀,因为服务器不能识别的http 400。
CrawlerHttpClient的使用和普通HttpClient没有区别,只是她需要CrawlerHttpClientBuilder来产生,如下:
private static CrawlerHttpClient crawlerHttpClient;
static {
SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setSoLinger(-1).setSoReuseAddress(false)
.setSoTimeout(ProxyConstant.SOCKETSO_TIMEOUT).setTcpNoDelay(true).build();
X509TrustManager x509mgr = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] xcs, String string) {
}
@Override
public void checkServerTrusted(X509Certificate[] xcs, String string) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { x509mgr }, null);
} catch (Exception e) {
//// TODO: 16/11/23
}
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
crawlerHttpClient = CrawlerHttpClientBuilder.create().setMaxConnTotal(1000).setMaxConnPerRoute(50)
.setDefaultSocketConfig(socketConfig).setSSLSocketFactory(sslConnectionSocketFactory)
.setRedirectStrategy(new LaxRedirectStrategy()).setDefaultCookieStore(new BarrierCookieStore()).build();
}
以上代码摘抄自com.virjar.dungproxy.client.httpclient.HttpInvoker
CrawlerHttpClient提供的扩展功能:
public static String postJSON(String url, Object entity, Header[] headers)
public static String get(String url)
public static String get(String url, Charset charset)
public static String get(String url, Charset charset, Header[] headers)
public static String get(String url, Charset charset, Header[] headers, String proxyIp, int proxyPort)
public static String get(String url, Charset charset, String proxyIp, int proxyPort)
public static String get(String url, Header[] headers)
public static String get(String url, Header[] headers, String proxyIp, int proxyPort)
public static String get(String url, HttpClientContext httpClientContext)
public static String get(String url, List<NameValuePair> nameValuePairs, Header[] headers, String proxyIp,int proxyPort)
public static String get(String url, List<NameValuePair> params)
public static String get(String url, List<NameValuePair> params, Charset charset)
public static String get(String url, List<NameValuePair> params, Charset charset, Header[] headers)
public static String get(String url, List<NameValuePair> params, Charset charset, Header[] headers, String proxyIp, int proxyPort)
public static String get(String url, List<NameValuePair> params, Charset charset, Header[] headers, String proxyIp, int proxyPort, HttpClientContext httpClientContext)
public static String get(String url, List<NameValuePair> params, Charset charset, String proxyIp, int proxyPort)
public static String get(String url, List<NameValuePair> params, Header[] headers)
public static String get(String url, List<NameValuePair> params, String proxyIp, int proxyPort)
public static String get(String url, Map<String, String> params, Charset charset, Header[] headers, String proxyIp, int proxyPort)
public static String get(String url, String proxyIp, int proxyPort)
public static int getStatus(String url, String proxyIp, int proxyPort)
public static String post(String url, HttpEntity entity, Charset charset, Header[] headers, String proxyIp, int proxyPort)
public static String post(String url, String entity)
public static String post(String url, String entity, Charset charset, Header[] headers, String proxyIp,int proxyPort)
public static String post(String url, String entity, Header[] headers)
public static String post(String url, List<NameValuePair> params)
public static String post(String url, List<NameValuePair> params, Charset charset, Header[] headers, String proxyIp,int proxyPort)
public static String post(String url, List<NameValuePair> params, Header[] headers)
public static String post(String url, Map<String, String> params)
public static String post(String url, Map<String, String> params, Charset charset, Header[] headers, String proxyIp,int proxyPort)
public static String post(String url, Map<String, String> params, Header[] headers)
public static String postJSON(String url, Object entity)
public static String postJSON(String url, Object entity, Charset charset, Header[] headers, String proxyIp,int proxyPort)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。