Beispiel #1
0
 static {
   HostConfiguration hostConfiguration = new HostConfiguration();
   HttpConnectionManagerParams params = new HttpConnectionManagerParams();
   params.setConnectionTimeout(3000);
   params.setMaxTotalConnections(2000);
   params.setStaleCheckingEnabled(true);
   params.setTcpNoDelay(true);
   params.setSoTimeout(10000);
   params.setMaxConnectionsPerHost(hostConfiguration, 1000);
   HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
   connectionManager.setParams(params);
   httpClient = new HttpClient(connectionManager);
   httpClient.setHostConfiguration(hostConfiguration);
 }
Beispiel #2
0
  /** 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;
  }
  public static HttpClient getClient() {

    int connectionTimeout = 3000;
    int pageTimeout = 7000;
    HttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
    HttpConnectionManagerParams connectionParams = connectionManager.getParams();
    connectionParams.setConnectionTimeout(connectionTimeout);
    connectionParams.setSoTimeout(pageTimeout);

    HttpClient httpClient = null;
    if (connectionManager != null) {
      httpClient = new HttpClient(connectionManager);
    }
    return httpClient;
  }
  public HostConfiguration getHostConfiguration(String location) throws IOException {

    if (_log.isDebugEnabled()) {
      _log.debug("Location is " + location);
    }

    HostConfiguration hostConfiguration = new HostConfiguration();

    hostConfiguration.setHost(new URI(location, false));

    if (isProxyHost(hostConfiguration.getHost())) {
      hostConfiguration.setProxy(_PROXY_HOST, _PROXY_PORT);
    }

    HttpConnectionManager httpConnectionManager = _httpClient.getHttpConnectionManager();

    HttpConnectionManagerParams httpConnectionManagerParams = httpConnectionManager.getParams();

    int defaultMaxConnectionsPerHost =
        httpConnectionManagerParams.getMaxConnectionsPerHost(hostConfiguration);

    int maxConnectionsPerHost =
        GetterUtil.getInteger(
            PropsUtil.get(
                HttpImpl.class.getName() + ".max.connections.per.host",
                new Filter(hostConfiguration.getHost())));

    if ((maxConnectionsPerHost > 0) && (maxConnectionsPerHost != defaultMaxConnectionsPerHost)) {

      httpConnectionManagerParams.setMaxConnectionsPerHost(
          hostConfiguration, maxConnectionsPerHost);
    }

    int timeout =
        GetterUtil.getInteger(
            PropsUtil.get(
                HttpImpl.class.getName() + ".timeout", new Filter(hostConfiguration.getHost())));

    if (timeout > 0) {
      HostParams hostParams = hostConfiguration.getParams();

      hostParams.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, timeout);
      hostParams.setIntParameter(HttpConnectionParams.SO_TIMEOUT, timeout);
    }

    return hostConfiguration;
  }
Beispiel #5
0
  static {
    HttpConnectionManagerParams params = loadHttpConfFromFile();

    connectionManager = new MultiThreadedHttpConnectionManager();

    connectionManager.setParams(params);

    client = new HttpClient(connectionManager);
  }
Beispiel #6
0
  /*     */ static
  /*     */ {
    /*  39 */ HttpConnectionManagerParams params = loadHttpConfFromFile();
    /*     */
    /*  41 */ connectionManager = new MultiThreadedHttpConnectionManager();
    /*     */
    /*  43 */ connectionManager.setParams(params);

    client = new HttpClient(connectionManager);
    /*     */ }
  /** 私有的构造方法 */
  private HttpProtocolHandler() {
    // 创建一个线程安全的HTTP连接池
    connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.getParams().setDefaultMaxConnectionsPerHost(defaultMaxConnPerHost);
    connectionManager.getParams().setMaxTotalConnections(defaultMaxTotalConn);

    IdleConnectionTimeoutThread ict = new IdleConnectionTimeoutThread();
    ict.addConnectionManager(connectionManager);
    ict.setConnectionTimeout(defaultIdleConnTimeout);

    ict.start();
  }
  /* (non-Javadoc)
   * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
   */
  public void afterPropertiesSet() throws Exception {
    final HttpConnectionManager httpConnectionManager = httpClient.getHttpConnectionManager();
    final HttpConnectionManagerParams params = httpConnectionManager.getParams();
    params.setConnectionTimeout(connectionTimeout);
    params.setSoTimeout(readTimeout);

    params.setParameter(
        HttpMethodParams.RETRY_HANDLER,
        new HttpMethodRetryHandler() {
          public boolean retryMethod(
              final HttpMethod method, final IOException exception, int executionCount) {
            if (executionCount >= timesToRetry) {
              // Do not retry if over max retry count
              return false;
            }
            if (exception instanceof NoHttpResponseException) {
              // Retry if the server dropped connection on us
              return true;
            }
            if (exception instanceof SocketException) {
              // Retry if the server reset connection on us
              return true;
            }
            if (exception instanceof SocketTimeoutException) {
              // Retry if the read timed out
              return true;
            }
            if (!method.isRequestSent()) {
              // Retry if the request has not been sent fully or
              // if it's OK to retry methods that have been sent
              return true;
            }
            // otherwise do not retry
            return false;
          }
        });
  }
  /**
   * Creates a new connection to the server.
   *
   * @param builder The HttpFileSystemConfigBuilder.
   * @param scheme The protocol.
   * @param hostname The hostname.
   * @param port The port number.
   * @param username The username.
   * @param password The password
   * @param fileSystemOptions The file system options.
   * @return a new HttpClient connection.
   * @throws FileSystemException if an error occurs.
   * @since 2.0
   */
  public static HttpClient createConnection(
      HttpFileSystemConfigBuilder builder,
      String scheme,
      String hostname,
      int port,
      String username,
      String password,
      FileSystemOptions fileSystemOptions)
      throws FileSystemException {
    HttpClient client;
    try {
      HttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
      HttpConnectionManagerParams connectionMgrParams = mgr.getParams();

      client = new HttpClient(mgr);

      final HostConfiguration config = new HostConfiguration();
      config.setHost(hostname, port, scheme);

      if (fileSystemOptions != null) {
        String proxyHost = builder.getProxyHost(fileSystemOptions);
        int proxyPort = builder.getProxyPort(fileSystemOptions);

        if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) {
          config.setProxy(proxyHost, proxyPort);
        }

        UserAuthenticator proxyAuth = builder.getProxyAuthenticator(fileSystemOptions);
        if (proxyAuth != null) {
          UserAuthenticationData authData =
              UserAuthenticatorUtils.authenticate(
                  proxyAuth,
                  new UserAuthenticationData.Type[] {
                    UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD
                  });

          if (authData != null) {
            final UsernamePasswordCredentials proxyCreds =
                new UsernamePasswordCredentials(
                    UserAuthenticatorUtils.toString(
                        UserAuthenticatorUtils.getData(
                            authData, UserAuthenticationData.USERNAME, null)),
                    UserAuthenticatorUtils.toString(
                        UserAuthenticatorUtils.getData(
                            authData, UserAuthenticationData.PASSWORD, null)));

            AuthScope scope = new AuthScope(proxyHost, AuthScope.ANY_PORT);
            client.getState().setProxyCredentials(scope, proxyCreds);
          }

          if (builder.isPreemptiveAuth(fileSystemOptions)) {
            HttpClientParams httpClientParams = new HttpClientParams();
            httpClientParams.setAuthenticationPreemptive(true);
            client.setParams(httpClientParams);
          }
        }

        Cookie[] cookies = builder.getCookies(fileSystemOptions);
        if (cookies != null) {
          client.getState().addCookies(cookies);
        }
      }
      /**
       * ConnectionManager set methodsmust be called after the host & port and proxy host & port are
       * set in the HostConfiguration. They are all used as part of the key when
       * HttpConnectionManagerParams tries to locate the host configuration.
       */
      connectionMgrParams.setMaxConnectionsPerHost(
          config, builder.getMaxConnectionsPerHost(fileSystemOptions));
      connectionMgrParams.setMaxTotalConnections(builder.getMaxTotalConnections(fileSystemOptions));

      client.setHostConfiguration(config);

      if (username != null) {
        final UsernamePasswordCredentials creds =
            new UsernamePasswordCredentials(username, password);
        AuthScope scope = new AuthScope(hostname, AuthScope.ANY_PORT);
        client.getState().setCredentials(scope, creds);
      }

      client.executeMethod(new HeadMethod());
    } catch (final Exception exc) {
      throw new FileSystemException(
          "vfs.provider.http/connect.error", new Object[] {hostname}, exc);
    }

    return client;
  }
Beispiel #10
0
  private String send(HttpMethod method, boolean sslExceptionIgnore) throws HttpNetAgentException {
    String responseBody = null;
    HttpClient hc = null;

    try {
      this.hashCode();
      String reqLog =
          ">> " + (method instanceof PostMethod ? "POST" : "GET") + " [" + method.getURI() + "]";

      if (method instanceof PostMethod) {
        NameValuePair[] params = ((PostMethod) method).getParameters();
        for (int i = 0; i < params.length; i++) {
          NameValuePair param = params[i];
          reqLog += "\n" + (param.getName() + "=" + param.getValue());
        }
      }
      reqLog +=
          ", hscd["
              + this.hashCode()
              + "], sslExcIgnore["
              + sslExceptionIgnore
              + "], hs["
              + this.hashCode()
              + "]";

      logger.info(reqLog);
      logger.info(getHttpInfoDumy(method));

      HttpConnectionManager httpConnMgr = new SimpleHttpConnectionManager();
      HttpConnectionManagerParams httpConnMgrParams = new HttpConnectionManagerParams();

      // Connection Timeout 설정
      httpConnMgrParams.setConnectionTimeout(netTimeoutConn);

      // Socket Timeout 설정
      httpConnMgrParams.setSoTimeout(netTimeoutSock);
      httpConnMgr.setParams(httpConnMgrParams);

      hc = new HttpClient(httpConnMgr);
      /*
      if (sslExceptionIgnore){
      	try {
      		Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443));
      	} catch (GeneralSecurityException e) {
      		e.printStackTrace();
      	}
      }
      */

      int status = hc.executeMethod(method);

      if (200 == status) {
        responseBody = method.getResponseBodyAsString();
        logger.info("<< responseBody [" + responseBody + "], hs[" + this.hashCode() + "]");
        if (responseBody == null) responseBody = "";

      } else {
        logger.error(
            "<< http status Not Success ["
                + status
                + "], hs["
                + this.hashCode()
                + "],"
                + getHttpInfoDumy(method));
        throw new HttpNetAgentException(
            HttpNetAgentException.HTTP_HAEDER_NOT_SUCCESS, "Http Status[" + status + "]");
      }

    } catch (HttpException e) {
      logger.error(
          "<< HttpException msg ["
              + e.getMessage()
              + "], hs["
              + this.hashCode()
              + "],"
              + getHttpInfoDumy(method),
          e);
      throw new HttpNetAgentException(HttpNetAgentException.HTTP_NET_ERROR, e.getMessage());
    } catch (IOException e) {
      logger.error(
          "<< IOException msg ["
              + e.getMessage()
              + "], hs["
              + this.hashCode()
              + "],"
              + getHttpInfoDumy(method),
          e);
      throw new HttpNetAgentException(HttpNetAgentException.HTTP_NET_ERROR, e.getMessage());
    }

    return responseBody;
  }
 public HttpClient getHttpClient() {
   HttpConnectionManagerParams params = new HttpConnectionManagerParams();
   HttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
   manager.setParams(params);
   return new HttpClient(manager);
 }