public static HttpClient getHttpClient(final String name) {
PoolingClientConnectionManager manager = getPoolingConnectionManager(500);
HttpClient client = new DefaultHttpClient(manager) {
{
List<String> uAs = userAgents.get(name);
int size = uAs.size();
long time = System.currentTimeMillis();
int index = (int) (time % size);
this.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
uAs.get((int) (time % size)));
(this.getParams())
.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
60000)
.setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000)
.setIntParameter(ClientPNames.MAX_REDIRECTS, 3);
this.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(
5, true));
}
};
try {
SchemeRegistry sRegistry = client.getConnectionManager()
.getSchemeRegistry();
SSLContext ctx = SSLContext.getInstance("SSL");//TLS
// Implementation of a trust manager for X509 certificates
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
sRegistry.register(new Scheme("https", 443, ssf));
sRegistry.register(new Scheme("http", 80, PlainSocketFactory
.getSocketFactory()));
// HttpHost host = new HttpHost("121.229.115.167", 8118);
// this.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
// host);
// this.setRoutePlanner(new HttpRoutePlanner(){
//
// private final String[] HOSTS_NEED_PROXY = new
// String[]{".sohu.com"};
// @Override
// public HttpRoute determineRoute(HttpHost target, HttpRequest
// request, HttpContext context) throws HttpException {
// String host = target.getHostName();
// boolean proxy = false;
// for(int index = 0 ; index < HOSTS_NEED_PROXY.length &&
// !proxy; ++index)
// {
// if(host.indexOf(HOSTS_NEED_PROXY[index])>0)proxy=true;
// }
// return proxy?
// new HttpRoute(target, null,
// proxies.get(count.get()),"https".equalsIgnoreCase(target.getSchemeName()))
// :new
// HttpRoute(target,/*IPs.random()*/null,"https".equalsIgnoreCase(target.getSchemeName()));
// }
// });
} catch (KeyManagementException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
return client;
}
httpclient支持https,http请求
最新推荐文章于 2024-05-12 12:25:12 发布