Last active
January 29, 2021 00:57
-
-
Save shamsimam/1a632dcb2d77291e60979fee0abad736 to your computer and use it in GitHub Desktop.
Apache HttpClient overriding socket creation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package shams; | |
| import org.apache.http.HttpResponse; | |
| import org.apache.http.client.HttpClient; | |
| import org.apache.http.client.methods.HttpGet; | |
| import org.apache.http.client.params.ClientPNames; | |
| import org.apache.http.conn.ClientConnectionManager; | |
| import org.apache.http.conn.ClientConnectionManagerFactory; | |
| import org.apache.http.conn.ConnectTimeoutException; | |
| import org.apache.http.conn.scheme.Scheme; | |
| import org.apache.http.conn.scheme.SchemeRegistry; | |
| import org.apache.http.conn.scheme.SchemeSocketFactory; | |
| import org.apache.http.conn.scheme.SocketFactory; | |
| import org.apache.http.impl.client.DefaultHttpClient; | |
| import org.apache.http.impl.conn.BasicClientConnectionManager; | |
| import org.apache.http.impl.conn.SchemeRegistryFactory; | |
| import org.apache.http.params.HttpParams; | |
| import java.io.IOException; | |
| import java.net.InetAddress; | |
| import java.net.InetSocketAddress; | |
| import java.net.Socket; | |
| import java.net.UnknownHostException; | |
| import java.util.List; | |
| /** | |
| * @author Shams Imam | |
| */ | |
| public class SocketMain { | |
| public static SchemeSocketFactory wrapSocketFactory(SchemeSocketFactory innerSocketFactory) { | |
| return new SchemeSocketFactory() { | |
| @Override | |
| public Socket createSocket(HttpParams params) throws IOException { | |
| Socket socket = innerSocketFactory.createSocket(params); | |
| socket.setKeepAlive(true); | |
| return socket; | |
| } | |
| @Override | |
| public Socket connectSocket(Socket sock, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpParams params) throws IOException { | |
| Socket socket = innerSocketFactory.connectSocket(sock, remoteAddress, localAddress, params); | |
| socket.setKeepAlive(true); | |
| return socket; | |
| } | |
| @Override | |
| public boolean isSecure(Socket sock) throws IllegalArgumentException { | |
| return innerSocketFactory.isSecure(sock); | |
| } | |
| }; | |
| } | |
| public static void main(String[] args) throws IOException { | |
| System.out.println("SocketMain.main: Creating client"); | |
| HttpClient client = new DefaultHttpClient() { | |
| protected ClientConnectionManager createClientConnectionManager() { | |
| ClientConnectionManager connManager = super.createClientConnectionManager(); | |
| final SchemeRegistry registry = connManager.getSchemeRegistry(); | |
| final List<String> schemeNames = registry.getSchemeNames(); | |
| for (String schemeName : schemeNames) { | |
| final Scheme scheme = registry.getScheme(schemeName); | |
| final Scheme newScheme = | |
| new Scheme(schemeName, | |
| scheme.getDefaultPort(), | |
| wrapSocketFactory(scheme.getSchemeSocketFactory())); | |
| registry.register(newScheme); | |
| } | |
| return connManager; | |
| } | |
| }; | |
| System.out.println("SocketMain.main: Opening connection"); | |
| HttpGet request = new HttpGet("http://www.google.com"); | |
| System.out.println("SocketMain.main: Awaiting response code"); | |
| HttpResponse response = client.execute(request); | |
| final int responseCode = response.getStatusLine().getStatusCode(); | |
| System.out.println("SocketMain.main: Response code = " + responseCode); | |
| response.getEntity().getContent().close(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment