Java Socks Proxy Socket Error
- by Ionut Ungureanu
I am trying to create a http request through a SOCKS (v4 / v5) proxy in Java. After reading about socks communication protocol on WikiPedia, I have put togheter this piece of code:
Socket sock = new Socket();
InetSocketAddress remoteProxyAddress = new InetSocketAddress(proxy ip, proxy port);
sock.connect(remoteProxyAddress, connTimeout);
InputStream in = sock.getInputStream();
OutputStream out = sock.getOutputStream();
out.write(0x04);
out.write(0x01);
out.write((endpoint.getPort() >> 8) & 0xff);
out.write((endpoint.getPort() >> 0) & 0xff);
out.write(endpoint.getAddress().getAddress());
out.write(0x0);
out.flush();
And here comes the part where I read from the proxy server. The problem is that the response is always "-1".
I have tried the proxy on Firefox and it works perfect. So... the problem is in my app.
Can anyone help me?
Thanks!