Exemple #1
0
  public void init() {
    HttpConnectionParams httpParams = new HttpConnectionParams();
    httpParams.setConnectionTimeout(httpConnectionTimeout);
    /* 连接超时 */
    httpParams.setSoTimeout(socketTimeout);

    Protocol myhttps = new Protocol("https", new MySSLSocketFactory(), 443);
    Protocol.registerProtocol("https", myhttps);
    connManager = new MultiThreadedHttpConnectionManager();
    connManager.setMaxTotalConnections(maxTotalConnections);
    connManager.setMaxConnectionsPerHost(40);
  }
 /**
  * Attempts to get a new socket connection to the given host within the given time limit.
  *
  * <p>To circumvent the limitations of older JREs that do not support connect timeout a controller
  * thread is executed. The controller thread attempts to create a new socket within the given
  * limit of time. If socket constructor does not return until the timeout expires, the controller
  * terminates and throws an {@link ConnectTimeoutException}
  *
  * @param host the host name/IP
  * @param port the port on the host
  * @param localAddress the local host name/IP to bind the socket to
  * @param localPort the port on the local machine
  * @param params {@link HttpConnectionParams Http connection parameters}
  * @return Socket a new socket
  * @throws IOException if an I/O error occurs while creating the socket
  * @throws UnknownHostException if the IP address of the host cannot be determined
  */
 @Override
 public Socket createSocket(
     final String host,
     final int port,
     final InetAddress localAddress,
     final int localPort,
     final HttpConnectionParams params)
     throws IOException, UnknownHostException, ConnectTimeoutException {
   if (params == null) {
     throw new IllegalArgumentException("Parameters may not be null");
   }
   int timeout = params.getConnectionTimeout();
   SocketFactory socketfactory = getSSLContext().getSocketFactory();
   if (timeout == 0) {
     Socket socket = socketfactory.createSocket(host, port, localAddress, localPort);
     if (socket instanceof SSLSocket) {
       ((SSLSocket) socket)
           .setEnabledProtocols(
               SSLUtils.getSupportedProtocols(((SSLSocket) socket).getEnabledProtocols()));
     }
     return socket;
   } else {
     Socket socket = socketfactory.createSocket();
     if (socket instanceof SSLSocket) {
       ((SSLSocket) socket)
           .setEnabledProtocols(
               SSLUtils.getSupportedProtocols(((SSLSocket) socket).getEnabledProtocols()));
     }
     SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
     SocketAddress remoteaddr = new InetSocketAddress(host, port);
     socket.bind(localaddr);
     socket.connect(remoteaddr, timeout);
     return socket;
   }
 }
 @Override
 public Socket createSocket(
     final String host,
     final int port,
     final InetAddress localAddress,
     final int localPort,
     final HttpConnectionParams params)
     throws IOException, UnknownHostException, ConnectTimeoutException {
   final int timeout = params.getConnectionTimeout();
   if (timeout == 0) {
     Socket socket = createSocket(host, port, localAddress, localPort);
     if (socket instanceof SSLSocket) {
       ((SSLSocket) socket)
           .setEnabledProtocols(
               SSLUtils.getSupportedProtocols(((SSLSocket) socket).getEnabledProtocols()));
     }
     return socket;
   } else {
     final Socket s = ssf.createSocket();
     if (s instanceof SSLSocket) {
       ((SSLSocket) s)
           .setEnabledProtocols(
               SSLUtils.getSupportedProtocols(((SSLSocket) s).getEnabledProtocols()));
     }
     s.bind(new InetSocketAddress(localAddress, localPort));
     s.connect(new InetSocketAddress(host, port), timeout);
     return s;
   }
 }
Exemple #4
0
  /**
   * Attempts to get a new socket connection to the given host within the given time limit.
   *
   * <p>This method employs several techniques to circumvent the limitations of older JREs that do
   * not support connect timeout. When running in JRE 1.4 or above reflection is used to call
   * Socket#connect(SocketAddress endpoint, int timeout) method. When executing in older JREs a
   * controller thread is executed. The controller thread attempts to create a new socket within the
   * given limit of time. If socket constructor does not return until the timeout expires, the
   * controller terminates and throws an {@link ConnectTimeoutException}
   *
   * @param host the host name/IP
   * @param port the port on the host
   * @param clientHost the local host name/IP to bind the socket to
   * @param clientPort the port on the local machine
   * @param params {@link HttpConnectionParams Http connection parameters}
   * @return Socket a new socket
   * @throws IOException if an I/O error occurs while creating the socket
   * @throws UnknownHostException if the IP address of the host cannot be determined
   */
  @Override
  public Socket createSocket(
      final String host,
      final int port,
      final InetAddress localAddress,
      final int localPort,
      final HttpConnectionParams params)
      throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
      throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    if (timeout == 0) {
      return createSocket(host, port, localAddress, localPort);
    }
    // To be eventually deprecated when migrated to Java 1.4 or above
    Socket socket =
        ReflectionSocketFactory.createSocket(
            "javax.net.ssl.SSLSocketFactory", host, port, localAddress, localPort, timeout);
    if (socket == null) {
      socket =
          ControllerThreadSocketFactory.createSocket(
              this, host, port, localAddress, localPort, timeout);
    }

    return socket;
  }
 @Override
 public Socket createSocket(
     String s, int i, InetAddress inetAddress, int i1, HttpConnectionParams httpConnectionParams)
     throws IOException, UnknownHostException, ConnectTimeoutException {
   int timeout = httpConnectionParams.getConnectionTimeout();
   Socket socket = _sslContext.getSocketFactory().createSocket();
   SocketAddress localaddr = new InetSocketAddress(inetAddress, i1);
   SocketAddress remoteaddr = new InetSocketAddress(s, i);
   socket.bind(localaddr);
   socket.connect(remoteaddr, timeout);
   return socket;
 }
  /**
   * Attempts to get a new socket connection to the given host within the given time limit.
   *
   * @param host the host name/IP
   * @param port the port on the host
   * @param clientHost the local host name/IP to bind the socket to
   * @param clientPort the port on the local machine
   * @param params {@link HttpConnectionParams Http connection parameters}
   * @return Socket a new socket
   * @throws IOException if an I/O error occurs while creating the socket
   * @throws UnknownHostException if the IP address of the host cannot be determined
   */
  public Socket createSocket(
      final String host,
      final int port,
      final InetAddress localAddress,
      final int localPort,
      final HttpConnectionParams params)
      throws IOException, UnknownHostException, ConnectTimeoutException {
    Log_OC.d(
        TAG,
        "Creating SSL Socket with remote "
            + host
            + ":"
            + port
            + ", local "
            + localAddress
            + ":"
            + localPort
            + ", params: "
            + params);
    if (params == null) {
      throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();

    // logSslInfo();

    SocketFactory socketfactory = mSslContext.getSocketFactory();
    Log_OC.d(
        TAG,
        " ... with connection timeout " + timeout + " and socket timeout " + params.getSoTimeout());
    Socket socket = socketfactory.createSocket();
    SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
    SocketAddress remoteaddr = new InetSocketAddress(host, port);
    socket.setSoTimeout(params.getSoTimeout());
    socket.bind(localaddr);
    ServerNameIndicator.setServerNameIndication(host, (SSLSocket) socket);
    socket.connect(remoteaddr, timeout);
    verifyPeerIdentity(host, port, socket);
    return socket;
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket
   * (java.lang.String, int, java.net.InetAddress, int,
   * org.apache.commons.httpclient.params.HttpConnectionParams)
   */
  public Socket createSocket(
      final String host,
      final int port,
      final InetAddress localHost,
      final int localPort,
      final HttpConnectionParams params)
      throws IOException, UnknownHostException {
    Socket socket = createSocket();

    int timeout = params.getConnectionTimeout();
    SocketAddress remoteaddr = new InetSocketAddress(host, port);
    SocketAddress localaddr = new InetSocketAddress(localHost, localPort);

    connectSocket(socket, remoteaddr, localaddr, timeout);
    return socket;
  }
 public Socket createSocket(
     String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params)
     throws IOException, UnknownHostException, ConnectTimeoutException {
   if (params == null) {
     throw new IllegalArgumentException("Parameters may not be null");
   }
   int timeout = params.getConnectionTimeout();
   SocketFactory socketfactory = getSSLContext().getSocketFactory();
   if (timeout == 0) {
     return socketfactory.createSocket(host, port, localAddress, localPort);
   } else {
     Socket socket = socketfactory.createSocket();
     SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
     SocketAddress remoteaddr = new InetSocketAddress(host, port);
     socket.bind(localaddr);
     socket.connect(remoteaddr, timeout);
     return socket;
   }
 }