Java 无法通过代理发出请求
我想通过代理提出请求。这在没有代理的情况下运行良好:Java 无法通过代理发出请求,java,proxy,Java,Proxy,我想通过代理提出请求。这在没有代理的情况下运行良好: 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()); oStre
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);
//............................
但是,使用代理时,此功能无法正常工作,它挂起:
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();
//............................
这是怎么回事?正如@MrSimpleMind已经说过的:使用
Proxy.Type.HTTP
而不是Proxy.Type.SOCKS你确定Proxy.Type.SOCKS而不是Proxy.Type.HTTP吗?@MrSimpleMind,对不起,我使用“HTTP”时的错误是java.lang.IllegalArgumentException:无效Proxy@MrSimpleMind,代理本身存在并且工作正常。是否尝试绑定?当使用带有系统属性的jvm代理时,它会运行吗?例如,当您运行java时,标记为-Dhttp.proxyHost=您的代理ip
-Dhttp.proxyPort=端口
。。只是为了确保它真的有效