简体   繁体   中英

How to use a custom socketfactory in Apache HttpComponents

I have been trying to use a custom SocketFactory in the httpclient library from the Apache HTTPComponents project. So far without luck. I was expecting that I could just set a socket factory for a HttpClient instance, but it is obviously not so easy.

The documentation for HttpComponents at http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html does mention socket factories, but does not say how to use them.

Does anybody know how this is done?

We use a custom socket factory to allow HttpClient connections to connect to HTTPS URLs with untrusted certificates.

Here is how we did it:

  1. We adapted implementations of both the 'EasySSLProtocolSocketFactory' and 'EasyX509TrustManager' classes from the examples source directory referenced by Oleg.

  2. In our HttpClient startup code, we do the following to enable the new socket factory:

     HttpClient httpClient = new HttpClient(); Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443); Protocol.registerProtocol("https", easyhttps); 

So that any time we request an https:// URL, this socket factory is used.

oleg's answer is of course correct, I just wanted to put the information directly here, in case the link goes bad. In the code that creates a HttpClient, I use this code to let it use my socket factory:

    CustomSocketFactory socketFactory = new CustomSocketFactory();
    Scheme scheme = new Scheme("http", 80, socketFactory);
    httpclient.getConnectionManager().getSchemeRegistry().register(scheme);

CustomSocketFactory is my own socket factory, and I want to use it for normal HTTP traffic, that's why I use "http" and 80 as parameters.

My CustomSchemeSocketFactory looks similar to this:

public class CustomSchemeSocketFactory implements SchemeSocketFactory {

  @Override
  public Socket connectSocket( Socket socket, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpParams params ) throws IOException, UnknownHostException, ConnectTimeoutException {

    if (localAddress != null) {
      socket.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
      socket.bind(localAddress);
    }
    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    try {
        socket.setSoTimeout(soTimeout);
        socket.connect(remoteAddress, connTimeout );
    } catch (SocketTimeoutException ex) {
        throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
    }

    return socket;
  }

  @Override
  public Socket createSocket( HttpParams params ) throws IOException {
    // create my own socket and return it
  }

  @Override
  public boolean isSecure( Socket socket ) throws IllegalArgumentException {
    return false;
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM