Unable to make a request via proxy

2019-07-14 00:27发布

I want to make a request via a proxy. This, with no proxy, works fine:

Socket sock = new Socket("example.com", 80);
OutputStream oStream = sock.getOutputStream();

//Writing headers to send to the proxy

String request = rType + " " + uri + " HTTP/1.0";
oStream.write(request.getBytes());
oStream.write(endOfLine.getBytes());

String cmd = "host: "+ header.get("host");
oStream.write(cmd.getBytes());
oStream.write(endOfLine.getBytes());
System.out.println(cmd);

//............................

But this, with a proxy, isn't working well, it hangs:

Proxy prx = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("my proxy ip address", 1234)); // predefined ip and port
sock = new Socket(prx);
sock.connect(new InetSocketAddress("example.com", 80)); //hangs here
oStream = sock.getOutputStream();

//............................

What's up with that?

标签: java proxy
1条回答
走好不送
2楼-- · 2019-07-14 00:55

As @MrSimpleMind already stated: Use Proxy.Type.HTTP instead of Proxy.Type.SOCKS.

查看更多
登录 后发表回答