6370908: Add support for HTTP_CONNECT proxy in Socket class (original) (raw)

Damjan Jovanovic damjan.jov at gmail.com
Sun Feb 21 05:09:45 PST 2010


Hi

From http://bugs.sun.com/viewbug.do?bugid=6370908

This RFE is basically about getting a TCP socket to tunnel through an HTTP proxy using the HTTP CONNECT request.

I've found a hack to get this feature to work, using sun.net.* packages and lots of reflection. Would it be acceptable to use this solution (with some way to change socket identity) in a patch that adds a java.net.HttpSocketImpl class similar to the java.net.SocksSocketImpl class that's already used to tunnel through SOCKS proxies? If not, in what other way should such a patch be done?

Thank you Damjan Jovanovic

import java.net.; import java.io.; import java.lang.reflect.*;

public class TunnelProxy { private static Socket connectThroughHTTPProxy(String proxyHost, int proxyPort, String destinationHost, int destinationPort) throws Exception { URL destinationURL = new URL("http://" + destinationHost + ":" + destinationPort); sun.net.www.protocol.http.HttpURLConnection conn = new sun.net.www.protocol.http.HttpURLConnection( destinationURL, new java.net.Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort))); conn.setDoInput(true); conn.setDoOutput(true); conn.connect(); conn.doTunneling(); Field httpField = conn.getClass().getDeclaredField("http"); httpField.setAccessible(true); sun.net.www.http.HttpClient httpClient = (sun.net.www.http.HttpClient) httpField.get(conn); Field serverSocketField = sun.net.NetworkClient.class.getDeclaredField("serverSocket"); serverSocketField.setAccessible(true); Socket socket = (Socket) serverSocketField.get(httpClient); return socket; }

public static void main(String[] args) throws Exception {
    System.setProperty("java.net.useSystemProxies", "true");
    InputStream in = connectThroughHTTPProxy(args[0],

Integer.parseInt(args[1]), args[2], Integer.parseInt(args[3])).getInputStream(); byte[] bytes = new byte[1024]; int bytesRead; while ((bytesRead = in.read(bytes)) != -1) { System.out.print(new String(bytes)); } } }



More information about the net-dev mailing list