3

I am using sockets to connect to websites and APIs via HTTP (I know, this could be done easier with the URL class, but I had some reasons for choosing this way).

Now I want to implement a privacy feature, and so all the connections should be redirected through a proxy. But I am running into problems doing that.

I looked for and selected a free proxy, for testing purposes, from a Free Proxy List website. I chose one with a high uptime for my testing routines, but it hasn't worked out so far.

My source code:

URL url = new URL("someurl");
InetSocketAddress address = new InetSocketAddress("202.77.110.22", 8080); 
java.net.Proxy javaProxy = new Proxy(Proxy.Type.SOCKS, address);
Socket server = new Socket(javaProxy);
server.connect(new InetSocketAddress(url.getHost(), url.getPort()));

I want to connect to the URL via the proxy "202.77.110.22:8080". If I execute the code using Eclipse, I have to wait approximately five minutes and then receive the following error:

SocketException: Malformed reply from SOCKS server

When using Proxy.Type.HTTP instead of Proxy.Type.SOCKS, I receive the following error message:

IllegalArgumentException - Invalid Proxy

I know that (forms of) this question have already been asked on Stackoverflow, but none of the answers have helped me. Maybe I chose an unsupported proxy? It works while testing it using Apache HttpComponents.

All help appreciated.

5
  • execepthe last line which I changed to server.connect(new InetSocketAddress(url.getHost(), (url.getPort()==-1)?80:url.getPort())); Your code works in my machine Commented Mar 15, 2013 at 12:35
  • Have you checked that the proxy works by setting it in your browser? Commented Mar 15, 2013 at 12:39
  • The proxy works using Apache HttpComponents. @OmarMEBARKI I tried it, but I still got the same error - I have to wait 5 min and then receive a SocketException. My full code: paste.ubuntu.com/5616412 Can you post your full code and your output? Commented Mar 15, 2013 at 12:47
  • I am sorry, Actually it doesnot work. It cannot work because a socket needs only an IP adress. But an URL can have IP + file name ... Commented Mar 15, 2013 at 13:05
  • 8080 will be an HTTP proxy, not a SOCKS proxy, as the first exception indicates. Commented Jul 15, 2015 at 3:53

2 Answers 2

4

Prior to Java 8, raw TCP socket connectivity only works through SOCKS proxies - that's why you got IllegalArgumentException - Invalid Proxy. It was only in Java 8 that connection of raw sockets through HTTP proxies was added.

Sign up to request clarification or add additional context in comments.

Comments

-1

I think you need to get rid of this line:

InetSocketAddress address = new InetSocketAddress("202.77.110.22", 8080); 

And change this line:

java.net.Proxy javaProxy = new Proxy(Proxy.Type.SOCKS, address);

to:

java.net.Proxy javaProxy = new Proxy(Proxy.Type.SOCKS, "202.77.110.22:8080");

Because you can't specify a proxy as a "InetSocketAddress". Those are two different things.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.