private HttpClient getClient() {
    HttpClient httpClient = new HttpClient();
    httpClient.getParams().setParameter("http.protocol.content-charset", "gbk");
    httpClient.getParams().setSoTimeout(timeout);

    if (useSSL) {
      if (sslPort > 0) {
        Protocol myhttps =
            new Protocol(
                "https", (ProtocolSocketFactory) new EasySSLProtocolSocketFactory(), sslPort);
        Protocol.registerProtocol("https", myhttps);
      }
    }

    if (useProxy) {
      HostConfiguration hc = new HostConfiguration();
      hc.setProxy(proxyUrl, proxyPort);
      httpClient.setHostConfiguration(hc);
      if (proxyUser != null) {
        httpClient
            .getState()
            .setProxyCredentials(
                AuthScope.ANY, new UsernamePasswordCredentials(proxyUser, proxyPassword));
      }
    }

    return httpClient;
  }
 private void configureHttpClient() {
   MultiThreadedHttpConnectionManager connMgr = new MultiThreadedHttpConnectionManager();
   HttpConnectionManagerParams connParams = connMgr.getParams();
   connParams.setMaxTotalConnections(maxConnections);
   connParams.setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, maxConnections);
   connMgr.setParams(connParams);
   hc = new HttpClient(connMgr);
   // NOTE: These didn't seem to help in my initial testing
   //			hc.getParams().setParameter("http.tcp.nodelay", true);
   //			hc.getParams().setParameter("http.connection.stalecheck", false);
   if (proxyHost != null) {
     HostConfiguration hostConfig = new HostConfiguration();
     hostConfig.setProxy(proxyHost, proxyPort);
     hc.setHostConfiguration(hostConfig);
     log.info("Proxy Host set to " + proxyHost + ":" + proxyPort);
     if (proxyUser != null && !proxyUser.trim().equals("")) {
       if (proxyDomain != null) {
         hc.getState()
             .setProxyCredentials(
                 new AuthScope(proxyHost, proxyPort),
                 new NTCredentials(proxyUser, proxyPassword, proxyHost, proxyDomain));
       } else {
         hc.getState()
             .setProxyCredentials(
                 new AuthScope(proxyHost, proxyPort),
                 new UsernamePasswordCredentials(proxyUser, proxyPassword));
       }
     }
   }
 }
 /**
  * @param sHostname, the host name
  * @param sProxyURL The proxy port, null if you don't use a proxy
  * @param iProxyPort, proxy port if you use one of -1 if not
  * @return An host configuration
  */
 private static HostConfiguration getHostConfiguration(
     String sHostname, String sProxyURL, int iProxyPort) {
   HostConfiguration host = new HostConfiguration();
   host.setHost(sHostname);
   if (sProxyURL != null && iProxyPort > 0) {
     host.setProxy(
         ConfigurationManager.getProperty(CONF_NETWORK_PROXY_HOSTNAME),
         ConfigurationManager.getInt(CONF_NETWORK_PROXY_PORT));
   }
   return host;
 }
 private HttpClient createHttpClient() {
   HttpClient client = new HttpClient();
   client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
   client.getParams().setConnectionManagerTimeout(timeout);
   if (proxy) {
     HostConfiguration hcf = new HostConfiguration();
     hcf.setProxy(this.proxyUrl, this.proxyPort);
     client.setHostConfiguration(hcf);
   }
   return client;
 }
  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;
  }
  /**
   * 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;
  }
 protected void applyProxy(HttpMethodBase method, String host) {
   HostConfiguration hc = new HostConfiguration();
   hc.setHost(host);
   hc.setProxy(proxyHost, proxyPort);
   method.setHostConfiguration(hc);
 }
示例#8
0
 public static void addProxy(final HostConfiguration config) {
   final String host = System.getProperty(HTTP_PROXYHOST);
   final String port = System.getProperty(HTTP_PROXYPORT, "-1");
   if (host != null && host.trim().length() > 0) config.setProxy(host, Integer.valueOf(port));
 }