/** * @since 4.1 */ @Override public Socket connectSocket( final Socket socket, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException { Args.notNull(remoteAddress, "Remote address"); Args.notNull(params, "HTTP parameters"); final HttpHost host; if (remoteAddress instanceof HttpInetSocketAddress) { host = ((HttpInetSocketAddress) remoteAddress).getHttpHost(); } else { host = new HttpHost(remoteAddress.getHostName(), remoteAddress.getPort(), "https"); } final int socketTimeout = HttpConnectionParams.getSoTimeout(params); final int connectTimeout = HttpConnectionParams.getConnectionTimeout(params); socket.setSoTimeout(socketTimeout); return connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, null); }
@Override public Socket connectSocket( final Socket socket, final String host, final int port, final InetAddress local, final int localPort, final HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException { final InetAddress remote; if (this.nameResolver != null) { remote = this.nameResolver.resolve(host); } else { remote = InetAddress.getByName(host); } InetSocketAddress localAddress = null; if (local != null || localPort > 0) { localAddress = new InetSocketAddress(local, localPort > 0 ? localPort : 0); } final InetSocketAddress remoteAddress = new HttpInetSocketAddress( new HttpHost(host, port), remote, port); return connectSocket(socket, remoteAddress, localAddress, params); }
@Override public Socket connectSocket(Socket sock, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpParams params) throws IOException { if (remoteAddress == null) { throw new IllegalArgumentException("Target host may not be null."); } if (params == null) { throw new IllegalArgumentException("Parameters may not be null."); } if (sock == null) { sock = createSocket(null); } if ((localAddress != null) ) { sock.bind( localAddress ); } String hostName; if (remoteAddress instanceof HttpInetSocketAddress) { hostName = ((HttpInetSocketAddress) remoteAddress).getHttpHost().getHostName(); } else { hostName = resolveHostName(remoteAddress); } InetSocketAddress remoteAddr = remoteAddress; if (this.hostNameResolver != null) { remoteAddr = new InetSocketAddress(this.hostNameResolver.resolve(hostName), remoteAddress.getPort()); } int timeout = HttpConnectionParams.getConnectionTimeout(params); try { sock.connect(remoteAddr, timeout); } catch (SocketTimeoutException ex) { throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out"); } return sock; }