コード例 #1
0
 private static HttpClient createHttpClient(String userAgent) {
   HttpClient httpClient = new HttpClient();
   httpClient.setHttpConnectionManager(WebUtil.getConnectionManager());
   httpClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
   WebUtil.configureHttpClient(httpClient, userAgent);
   return httpClient;
 }
コード例 #2
0
ファイル: HttpClientFactory.java プロジェクト: tonyzyb/SOAPUI
  /** Factory method used by producers and consumers to create a new {@link HttpClient} instance */
  public static HttpClient createHttpClient() {
    HttpClientParams clientParams = new HttpClientParams();
    HttpConnectionManagerParams managerParams = new HttpConnectionManagerParams();
    managerParams.setConnectionTimeout(1000); // 设置连接超时时间
    managerParams.setDefaultMaxConnectionsPerHost(2);
    managerParams.setSoTimeout(1000); // 设置读取数据超时时间
    HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
    httpConnectionManager.setParams(managerParams);

    HttpClient answer = new HttpClient(clientParams);
    answer.setHttpConnectionManager(httpConnectionManager);

    return answer;
  }
コード例 #3
0
ファイル: HttpUtils.java プロジェクト: tiansiyuan/misc
 /** 初始化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()));
     }
   }
 }