I have a piece of code to connect to a Socket server, and it works fine.
Socket socket = new Socket();
socket.connect(new InetSocketAddress(address, port));
Now I want to connect via a HTTP proxy, what should I do?
I tried this and failed
SocketAddress proxyAddr = new InetSocketAddress(proxyHost, proxyPort);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);
Socket socket = new Socket(proxy);
socket.connect(new InetSocketAddress(address, port));
this post suggests that I should use Jakarta Commons HttpClient, but how to use it to connect a Socket server via the HTTP proxy?
UPDATED: I used SOCKS proxy and it doesn't work, if I use HTTP proxy:
SocketAddress proxyAddr = new InetSocketAddress(proxyHost, proxyPort);
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
Socket socket = new Socket(proxy);
socket.connect(new InetSocketAddress(address, port));
and it will throw an IllegalArgumentException
java.lang.IllegalArgumentException: Proxy is null or invalid type
at java.net.Socket.<init>(Socket.java:88)