Example #1
0
  /**
   * Returns the next connection to attempt.
   *
   * @throws NoSuchElementException if there are no more routes to attempt.
   */
  Connection nextUnconnected() throws IOException {
    // Always prefer pooled connections over new connections.
    for (Connection pooled; (pooled = pool.get(address)) != null; ) {
      if (request.method().equals("GET") || Internal.instance.isReadable(pooled)) return pooled;
      pooled.getSocket().close();
    }

    // Compute the next route to attempt.
    if (!hasNextTlsVersion()) {
      if (!hasNextInetSocketAddress()) {
        if (!hasNextProxy()) {
          if (!hasNextPostponed()) {
            throw new NoSuchElementException();
          }
          return new Connection(pool, nextPostponed());
        }
        lastProxy = nextProxy();
        resetNextInetSocketAddress(lastProxy);
      }
      lastInetSocketAddress = nextInetSocketAddress();
      resetNextTlsVersion();
    }

    String tlsVersion = nextTlsVersion();
    Route route = new Route(address, lastProxy, lastInetSocketAddress, tlsVersion);
    if (routeDatabase.shouldPostpone(route)) {
      postponedRoutes.add(route);
      // We will only recurse in order to skip previously failed routes. They will be
      // tried last.
      return nextUnconnected();
    }

    return new Connection(pool, route);
  }
 /** 配置Client对象 */
 private void configHttpClient() {
   this.client.setConnectTimeout(30, TimeUnit.SECONDS);
   this.client.setReadTimeout(30, TimeUnit.SECONDS);
   this.client.setWriteTimeout(30, TimeUnit.SECONDS);
   this.client.setConnectionPool(ConnectionPool.getDefault());
   this.client.getDispatcher().setMaxRequestsPerHost(3);
   this.client.getDispatcher().setMaxRequests(3);
   // 拦截器
   this.client.interceptors().add(new LoggingInterceptor());
 }