我正在尝试在运行时使用代理创建URL连接。 我的代码如下:
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.0.2.100", 80));
HttpURLConnection connection =
(HttpURLConnection)new URL("http://abc.example.com").openConnection(proxy);
但这并没有起作用。有人知道原因吗?
我正在尝试在运行时使用代理创建URL连接。 我的代码如下:
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.0.2.100", 80));
HttpURLConnection connection =
(HttpURLConnection)new URL("http://abc.example.com").openConnection(proxy);
但这并没有起作用。有人知道原因吗?
为了帮助未来的访问者添加答案
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.0.2.100", 80));
HttpURLConnection connection =(HttpURLConnection)new URL("http://abc.example.com").openConnection(proxy);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-type", "text/xml");
connection.setRequestProperty("Accept", "text/xml, application/xml");
connection.setRequestMethod("POST");
我尝试了这个方法,它可以实现。但是需要两倍的时间。
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.0.2.100", 80));
HttpURLConnection connection =
(HttpURLConnection)new URL("http://abc.example.com").openConnection(proxy);
((HttpURLConnection)new URL("http://abc.example.com").openConnection(proxy)).getInputStream();
System.out.println(connection.usingProxy());
结果为真
没有
((HttpURLConnection)new URL("http://abc.example.com").openConnection(proxy)).getInputStream();
结果为假