Java Socks Proxy Socket Error

Posted by Ionut Ungureanu on Stack Overflow See other posts from Stack Overflow or by Ionut Ungureanu
Published on 2012-09-29T15:15:22Z Indexed on 2012/09/29 15:37 UTC
Read the original article Hit count: 248

Filed under:
|
|
|

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!

© Stack Overflow or respective owner

Related posts about java

Related posts about sockets