org.apache.commons.httpclient.params.HttpConnectionParams源码实例Demo

类org.apache.commons.httpclient.params.HttpConnectionParams源码实例Demo

下面列出了org.apache.commons.httpclient.params.HttpConnectionParams 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: freeacs   文件: EasySSLProtocolSocketFactory.java
/**
 * 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 address
 * @param localPort the local port
 * @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
 * @throws ConnectTimeoutException the connect timeout exception
 */
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) {
    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;
  }
}
 
源代码2 项目: alfresco-core   文件: HttpClientFactory.java
protected HttpClient constructHttpClient()
{
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpClient httpClient = new HttpClient(connectionManager);
    HttpClientParams params = httpClient.getParams();
    params.setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true);
    params.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, true);
    if (socketTimeout != null) 
    {
        params.setSoTimeout(socketTimeout);
    }
    HttpConnectionManagerParams connectionManagerParams = httpClient.getHttpConnectionManager().getParams();
    connectionManagerParams.setMaxTotalConnections(maxTotalConnections);
    connectionManagerParams.setDefaultMaxConnectionsPerHost(maxHostConnections);
    connectionManagerParams.setConnectionTimeout(connectionTimeout);

    return httpClient;
}
 
源代码3 项目: javabase   文件: HttpClientUtil.java
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;
	}
}
 
源代码4 项目: http4e   文件: AuthSSLProtocolSocketFactory.java
/**
 * 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}
 * </p>
 *  
 * @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 {
    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;
    }
}
 
源代码5 项目: http4e   文件: SocketFactoryWrapper.java
public Socket createSocket(
        String host, 
        int port, InetAddress localAddress, int localPort,
        HttpConnectionParams params) throws IOException, UnknownHostException,
        ConnectTimeoutException {
    // Based on code from EasySSLProtocolSocketFactory.java
    Socket rval;
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    if (timeout == 0) {
        rval = socketFactory.createSocket(host, port, localAddress, localPort);
    } else {
        rval = socketFactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        rval.bind(localaddr);
        rval.connect(remoteaddr, timeout);
    }
    return rval;
}
 
源代码6 项目: http4e   文件: EasySSLProtocolSocketFactory.java
/**
 * 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}
 * </p>
 *  
 * @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 {
    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;
    }
}
 
/**
 * 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 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 {
    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;
    }
}
 
@Test
public void shouldCreateSocketWithGivenLocalAddressAndPort() throws Exception {
    // Given
    InetAddress localAddress = InetAddress.getLoopbackAddress();
    int localPort = 28080;
    // When
    Socket sslSocket =
            socketFactory.createSocket(
                    "localhost",
                    serverPort,
                    localAddress,
                    localPort,
                    new HttpConnectionParams());
    // Then
    assertThat(sslSocket.getLocalAddress(), is(equalTo(localAddress)));
    assertThat(sslSocket.getLocalPort(), is(equalTo(localPort)));
}
 
@Test
@Disabled(value = "Requires a way to slow down connect process artificially")
public void shouldFailCreatingSocketWithInstantTimeout() throws Exception {
    // Given
    HttpConnectionParams params = new HttpConnectionParams();
    params.setConnectionTimeout(1);
    // When / Then
    assertThrows(
            SocketTimeoutException.class,
            () ->
                    socketFactory.createSocket(
                            "localhost",
                            serverPort,
                            InetAddress.getLoopbackAddress(),
                            38080,
                            params));
}
 
源代码10 项目: sakai   文件: EasySSLProtocolSocketFactory.java
/**
 * 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}
 * </p>
 *  
 * @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 {
    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;
    }
}
 
源代码11 项目: olat   文件: HttpClientFactory.java
/**
 * A HttpClient with basic authentication and no host or port setting. Can only be used to retrieve absolute URLs
 * 
 * @param user
 *            can be NULL
 * @param password
 *            can be NULL
 * @return HttpClient
 */
public static HttpClient getHttpClientInstance(String user, String password) {
    HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionParams params = connectionManager.getParams();
    // wait max 10 seconds to establish connection
    params.setConnectionTimeout(10000);
    // a read() call on the InputStream associated with this Socket
    // will block for only this amount
    params.setSoTimeout(10000);
    HttpClient c = new HttpClient(connectionManager);

    // use basic authentication if available
    if (user != null && user.length() > 0) {
        AuthScope authScope = new AuthScope(null, -1, null);
        Credentials credentials = new UsernamePasswordCredentials(user, password);
        c.getState().setCredentials(authScope, credentials);
    }
    return c;
}
 
源代码12 项目: olat   文件: HttpClientFactory.java
/**
 * A HttpClient with basic authentication and no host or port setting. Can only be used to retrieve absolute URLs
 * 
 * @param user
 *            can be NULL
 * @param password
 *            can be NULL
 * @return HttpClient
 */
public static HttpClient getHttpClientInstance(String user, String password) {
    HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionParams params = connectionManager.getParams();
    // wait max 10 seconds to establish connection
    params.setConnectionTimeout(10000);
    // a read() call on the InputStream associated with this Socket
    // will block for only this amount
    params.setSoTimeout(10000);
    HttpClient c = new HttpClient(connectionManager);

    // use basic authentication if available
    if (user != null && user.length() > 0) {
        AuthScope authScope = new AuthScope(null, -1, null);
        Credentials credentials = new UsernamePasswordCredentials(user, password);
        c.getState().setCredentials(authScope, credentials);
    }
    return c;
}
 
/**
 * 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}
 * </p>
 *
 * @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
 */
public Socket createSocket(final String host,
                           final int port,
                           final InetAddress localAddress,
                           final int localPort,
                           final HttpConnectionParams params) throws IOException {
    if (params == null)
    {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    if (timeout == 0)
    {
        return createSocket(host, port, localAddress, localPort);
    }
    else
    {
        // To be eventually deprecated when migrated to Java 1.4 or above
        return ControllerThreadSocketFactory.createSocket(this, host, port, localAddress, localPort, timeout);
    }
}
 
public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort,
                           final HttpConnectionParams params) throws IOException {
	if (params == null) {
		throw new IllegalArgumentException("Parameters may not be null");
	}
	int timeout = params.getConnectionTimeout();
	SocketFactory socketfactory = ctx.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;
	}
}
 
源代码15 项目: sakai   文件: EasySSLProtocolSocketFactory.java
/**
 * 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}
 * </p>
 *  
 * @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 {
    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;
    }
}
 
源代码16 项目: elasticsearch-hadoop   文件: SocksSocketFactory.java
public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params)
        throws IOException, UnknownHostException, ConnectTimeoutException {

    InetSocketAddress socksAddr = new InetSocketAddress(socksHost, socksPort);
    Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksAddr);
    int timeout = params.getConnectionTimeout();

    Socket socket = new Socket(proxy);
    socket.setSoTimeout(timeout);

    SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
    SocketAddress remoteaddr = new InetSocketAddress(host, port);
    socket.bind(localaddr);
    socket.connect(remoteaddr, timeout);
    return socket;
}
 
源代码17 项目: elasticsearch-hadoop   文件: SSLSocketFactory.java
@Override
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;
    }
}
 
源代码18 项目: Spark   文件: EasySSLProtocolSocketFactory.java
/**
    * 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}
    * </p>
    *
    * @param host   the host name/IP
    * @param port   the port on the host
    * @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 {
       if (params == null) {
           throw new IllegalArgumentException("Parameters may not be null");
       }
       int timeout = params.getConnectionTimeout();
       if (timeout == 0) {
           return createSocket(host, port, localAddress, localPort);
       }
       else {
           // To be eventually deprecated when migrated to Java 1.4 or above
           return ControllerThreadSocketFactory.createSocket(
                   this, host, port, localAddress, localPort, timeout);
       }
   }
 
public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort, HttpConnectionParams params) throws IOException {
    try {
        return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException | InvalidAlgorithmParameterException e) {
        throw new IOException(e + " " + e.getMessage());
    }
}
 
源代码20 项目: Java_Certificado   文件: SocketFactoryDinamico.java
@Override
public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params) throws IOException {
    final Socket socket = this.ssl.getSocketFactory().createSocket();
    socket.bind(new InetSocketAddress(localAddress, localPort));
    socket.connect(new InetSocketAddress(host, port), 60000);
    return socket;
}
 
@VisibleForTesting
protected HTTPArtifactBinding createDefaultArtifactBinding(ServiceProviderBuilder builder) {
    HttpClientParams params = new HttpClientParams();
    params.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 60000);
    HttpClient httpClient = new HttpClient(params, new MultiThreadedHttpConnectionManager());
    ArtifactResolutionProfileImpl artifactResolutionProfile = new ArtifactResolutionProfileImpl(httpClient);
    builder.setSharedObject(ArtifactResolutionProfile.class, artifactResolutionProfile);
    HTTPSOAP11Binding soapBinding = new HTTPSOAP11Binding(parserPool);
    artifactResolutionProfile.setProcessor(new SAMLProcessorImpl(soapBinding));
    return new HTTPArtifactBinding(parserPool, getVelocityEngine(), artifactResolutionProfile);
}
 
源代码22 项目: http4e   文件: HttpMethodDirector.java
/**
 * Applies connection parameters specified for a given method
 * 
 * @param method HTTP method
 * 
 * @throws IOException if an I/O occurs setting connection parameters 
 */
private void applyConnectionParams(final HttpMethod method) throws IOException {
    int timeout = 0;
    // see if a timeout is given for this method
    Object param = method.getParams().getParameter(HttpMethodParams.SO_TIMEOUT);
    if (param == null) {
        // if not, use the default value
        param = this.conn.getParams().getParameter(HttpConnectionParams.SO_TIMEOUT);
    }
    if (param != null) {
        timeout = ((Integer)param).intValue();
    }
    this.conn.setSocketTimeout(timeout);                    
}
 
源代码23 项目: http4e   文件: StrictSSLProtocolSocketFactory.java
/**
 * 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}
 * </p>
 *  
 * @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 {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    Socket socket = null;
    
    SocketFactory socketfactory = SSLSocketFactory.getDefault();
    if (timeout == 0) {
        socket = socketfactory.createSocket(host, port, localAddress, localPort);
    } else {
        socket = socketfactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
    }
    verifyHostname((SSLSocket)socket);
    return socket;
}
 
public HttpConnectionParams getParams() {
    if (hasConnection()) {
        return wrappedConnection.getParams();
    } else {
        throw new IllegalStateException("Connection has been released");
    }
}
 
public void setParams(final HttpConnectionParams params) {
    if (hasConnection()) {
        wrappedConnection.setParams(params);
    } else {
        throw new IllegalStateException("Connection has been released");
    }
}
 
源代码26 项目: knopflerfish.org   文件: HttpMethodDirector.java
/**
 * Applies connection parameters specified for a given method
 * 
 * @param method HTTP method
 * 
 * @throws IOException if an I/O occurs setting connection parameters 
 */
private void applyConnectionParams(final HttpMethod method) throws IOException {
    int timeout = 0;
    // see if a timeout is given for this method
    Object param = method.getParams().getParameter(HttpMethodParams.SO_TIMEOUT);
    if (param == null) {
        // if not, use the default value
        param = this.conn.getParams().getParameter(HttpConnectionParams.SO_TIMEOUT);
    }
    if (param != null) {
        timeout = ((Integer)param).intValue();
    }
    this.conn.setSocketTimeout(timeout);                    
}
 
@Test
public void shouldFailCreatingSocketForUnknownHost() throws Exception {
    // Given
    String unknownHost = "localhorst";
    InetAddress localAddress = InetAddress.getLoopbackAddress();
    int localPort = 28080;
    HttpConnectionParams params = new HttpConnectionParams();
    params.setConnectionTimeout(60000);
    // When / Then
    assertThrows(
            IOException.class,
            () ->
                    socketFactory.createSocket(
                            unknownHost, serverPort, localAddress, localPort, params));
}
 
@Test
public void shouldFailCreatingSocketForMissingParameters() throws Exception {
    // Given
    HttpConnectionParams nullParams = null;
    // When / Then
    assertThrows(
            IllegalArgumentException.class,
            () ->
                    socketFactory.createSocket(
                            "localhost",
                            serverPort,
                            InetAddress.getLoopbackAddress(),
                            12345,
                            nullParams));
}
 
@Test
public void shouldSucceedCreatingSocketWithReasonableTimeout() throws Exception {
    // Given
    HttpConnectionParams params = new HttpConnectionParams();
    params.setConnectionTimeout(1000);
    // When
    Socket sslSocket =
            socketFactory.createSocket(
                    "localhost", serverPort, InetAddress.getLoopbackAddress(), 48080, params);
    // Then
    assertThat(sslSocket, is(notNullValue()));
}
 
源代码30 项目: cloudstack   文件: TrustingProtocolSocketFactory.java
@Override
public Socket createSocket(String host, int port, InetAddress localAddress,
        int localPort, HttpConnectionParams params) throws IOException,
        UnknownHostException, ConnectTimeoutException {
    int timeout = params.getConnectionTimeout();
    if (timeout == 0) {
        return createSocket(host, port, localAddress, localPort);
    }
    else {
        Socket s = ssf.createSocket();
        s.bind(new InetSocketAddress(localAddress, localPort));
        s.connect(new InetSocketAddress(host, port), timeout);
        return s;
    }
}
 
如果文章对您有帮助,请点击页面广告。以鼓励作者做出的努力。
 类方法
 同包方法