Oleg's answer, of course, is correct, I just wanted to post the information right here, in case the link goes wrong. In the code that HttpClient creates, I use this code so that it can use my factory socket:
CustomSocketFactory socketFactory = new CustomSocketFactory(); Scheme scheme = new Scheme("http", 80, socketFactory); httpclient.getConnectionManager().getSchemeRegistry().register(scheme);
CustomSocketFactory
is my own factory socket, and I want to use it for regular HTTP traffic, so I use "http"
and 80
as parameters.
My CustomSchemeSocketFactory looks something like 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 {
source share