如何在 java 中使用代理获取 URL 连接?

2022-01-24 00:00:00 connection proxy url java

我正在尝试在运行时使用代理创建 URL 连接.我的代码如下:

I am trying to create URL connection using a proxy at run time. My code is below:

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);

但这不起作用.有人知道为什么吗?

But this is not working. Anybody know why?

推荐答案

为以后的访客添加答案

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");

相关文章