private org.apache.commons.httpclient.HttpClient createHttpClient() { HttpClientParams clientParams = new HttpClientParams(); clientParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES); org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient(clientParams, connManager); return httpClient; }
public HttpClient(int maxConPerHost, int conTimeOutMs, int soTimeOutMs, int maxSize) { connectionManager = new MultiThreadedHttpConnectionManager(); HttpConnectionManagerParams params = connectionManager.getParams(); params.setDefaultMaxConnectionsPerHost(maxConPerHost); params.setConnectionTimeout(conTimeOutMs); params.setSoTimeout(soTimeOutMs); HttpClientParams clientParams = new HttpClientParams(); // 忽略cookie 避免 Cookie rejected 警告 clientParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES); client = new org.apache.commons.httpclient.HttpClient(clientParams, connectionManager); Protocol myhttps = new Protocol("https", new MySSLSocketFactory(), 443); Protocol.registerProtocol("https", myhttps); this.maxSize = maxSize; // 支持proxy if (proxyHost != null && !proxyHost.equals("")) { client.getHostConfiguration().setProxy(proxyHost, proxyPort); client.getParams().setAuthenticationPreemptive(true); if (proxyAuthUser != null && !proxyAuthUser.equals("")) { client .getState() .setProxyCredentials( AuthScope.ANY, new UsernamePasswordCredentials(proxyAuthUser, proxyAuthPassword)); log("Proxy AuthUser: "******"Proxy AuthPassword: " + proxyAuthPassword); } } }
/** 初始化httpclient */ static { int connectionTimeout = 30000; int soTimeout = 30000; try { connectionTimeout = Integer.parseInt(System.getProperty("sun.net.client.defaultConnectTimeout", "30000")); } catch (Exception e) { } try { soTimeout = Integer.parseInt(System.getProperty("sun.net.client.defaultReadTimeout", "30000")); } catch (Exception e) { } MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.getParams().setDefaultMaxConnectionsPerHost(10); connectionManager.getParams().setMaxTotalConnections(300); connectionManager.getParams().setConnectionTimeout(connectionTimeout); connectionManager.getParams().setSoTimeout(soTimeout); client.setHttpConnectionManager(connectionManager); // 忽略cookie 避免 Cookie rejected 警告 HttpClientParams clientParams = new HttpClientParams(); clientParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES); client.setParams(clientParams); // 支持https Protocol myhttps = new Protocol("https", new SSLSocketFactory(), 443); Protocol.registerProtocol("https", myhttps); // 设置代理 if (ProxyClient.getProxy() != null) { client.getHostConfiguration().setProxy(ProxyClient.getHost(), ProxyClient.getPort()); client.getParams().setAuthenticationPreemptive(true); if (ProxyClient.getUsername() != null && !ProxyClient.getUsername().trim().equals("")) { client .getState() .setProxyCredentials( AuthScope.ANY, new UsernamePasswordCredentials( ProxyClient.getUsername().trim(), ProxyClient.getPassword().trim())); } } }