3

I am trying to get Proxy working with Socket. But everytime I tried, it would returned a "Exception in thread "pool-1-thread-1" java.lang.IllegalArgumentException: Invalid Proxy" exception error

at java.net.Socket.(Socket.java:131)

But if its Proxy.Type.SOCKS, it works.

public void Test()
{
   Socket s = null;
   SocketAddress addr = null;
   Proxy proxy = null;

   addr = new InetSocketAddress("127.0.0.1", 8080);
   proxy = new Proxy(Proxy.Type.HTTP, addr);
   socket = new Socket(proxy); // This is the line that is triggering the exception
}
user303907
  • 533
  • 1
  • 9
  • 18
  • You are using localhost(127.0.0.1 port 8080) as proxy, are you running a proxy server in local? – Rajesh Pantula Jan 11 '12 at 08:58
  • replace InetSocketAddress("127.0.0.1", 8080) with your proxy server ip and port no. – Rajesh Pantula Jan 11 '12 at 08:59
  • Take a look here http://stackoverflow.com/questions/7225873/how-to-connect-a-socket-server-via-http-proxy – Aviram Segal Jan 11 '12 at 09:00
  • 1
    @rao_555 Yes, I have a proxy server (Fiddler2) running under localhost. I am also guessing that new Socket(Proxy p) constructor does not allow HTTP proxies to be used, only SOCKS. I have taken a look at Socket.java and if you used a Type.HTTP, it will throw an IllegalArgumentException right away. – user303907 Jan 11 '12 at 09:09
  • So was this ever solved for you? – David Feb 08 '21 at 18:24

1 Answers1

5

Sadly this is a bug in (Oracle) Java - only DIRECT and SOCKS proxy is supported for Socket. See http://bugs.sun.com/view_bug.do?bug_id=6370908.

Martin Ždila
  • 2,764
  • 3
  • 27
  • 34
  • Thanks. You saved me some ours of potentially frustrating debugging. Why the hell isn't this fixed. – kufi Mar 25 '14 at 09:45
  • Is this still not fixed? The bug ID shows resolved fixed back in 2013. Dependent on platform or Java version? – David Feb 08 '21 at 17:59