Java 如何让 HttpURLConnection 使用代理?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1432961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:02:32  来源:igfitidea点击:

How do I make HttpURLConnection use a proxy?

javawindowshttpproxy

提问by izb

If I do this...

如果我这样做...

conn = new URL(urlString).openConnection();
System.out.println("Proxy? " + conn.usingProxy());

it prints

它打印

Proxy? false

The problem is, I am behind a proxy. Where does the JVM get its proxy information from on Windows? How do I set this up? All my other apps seem perfectly happy with my proxy.

问题是,我支持代理。在 Windows 上,JVM 从哪里获取其代理信息?我该如何设置?我的所有其他应用程序似乎对我的代理非常满意。

采纳答案by NickDK

Since java 1.5 you can also pass a java.net.Proxyinstance to the openConnection(proxy)method:

从 java 1.5 开始,您还可以将java.net.Proxy实例传递给该openConnection(proxy)方法:

//Proxy instance, proxy ip = 10.0.0.1 with port 8080
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8080));
conn = new URL(urlString).openConnection(proxy);

If your proxy requires authentication it will give you response 407.

如果您的代理需要身份验证,它将为您提供响应 407。

In this case you'll need the following code:

在这种情况下,您将需要以下代码:

    Authenticator authenticator = new Authenticator() {

        public PasswordAuthentication getPasswordAuthentication() {
            return (new PasswordAuthentication("user",
                    "password".toCharArray()));
        }
    };
    Authenticator.setDefault(authenticator);

回答by Sean Owen

This is fairly easy to answer from the internet. Set system properties http.proxyHostand http.proxyPort. You can do this with System.setProperty(), or from the command line with the -Dsyntax.

这很容易从互联网上回答。设置系统属性http.proxyHosthttp.proxyPort. 您可以使用System.setProperty(), 或使用-D语法从命令行执行此操作。

回答by ZZ Coder

Set following before you openConnection,

在打开连接之前设置以下内容,

System.setProperty("http.proxyHost", "host");
System.setProperty("http.proxyPort", "port_number");

If proxy requires authentication,

如果代理需要身份验证,

System.setProperty("http.proxyUser", "user");
System.setProperty("http.proxyPassword", "password");

回答by Pascal Thivent

Proxies are supported through two system properties: http.proxyHost and http.proxyPort. They must be set to the proxy server and port respectively. The following basic example illustrates it:

通过两个系统属性支持代理:http.proxyHost 和 http.proxyPort。它们必须分别设置为代理服务器和端口。以下基本示例说明了它:

String url = "http://www.google.com/",
       proxy = "proxy.mydomain.com",
       port = "8080";
URL server = new URL(url);
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost",proxy);
systemProperties.setProperty("http.proxyPort",port);
HttpURLConnection connection = (HttpURLConnection)server.openConnection();
connection.connect();
InputStream in = connection.getInputStream();
readResponse(in);

回答by Daniel Worthington-Bodart

You can also set

你也可以设置

-Djava.net.useSystemProxies=true

On Windows and Linux this will use the system settings so you don't need to repeat yourself (DRY)

在 Windows 和 Linux 上,这将使用系统设置,因此您无需重复(DRY)

http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html#Proxies

http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html#Proxies

回答by Norbert

The approved answer will work ... if you know your proxy host and port =) . But in case you are looking for the proxy host and port the steps below should help

批准的答案将起作用...如果您知道您的代理主机和端口 =) 。但如果您正在寻找代理主机和端口,以下步骤应该会有所帮助

if auto configured proxy is given: then

1> open IE(or any browser)

2> get the url address from your browser through IE->Tools->internet option->connections->LAN Settings-> get address and give in url eg: as http://autocache.abc.com/and enter, a file will be downloaded with .pac format, save to desktop

3> open .pac file in textpad, identify PROXY:

In your editor, it will come something like:

return "PROXY web-proxy.ind.abc.com:8080; PROXY proxy.sgp.abc.com:8080";

如果给出了自动配置的代理:那么

1>打开IE(或任何浏览器)

2> 通过 IE->Tools->internet option->connections->LAN Settings-> 获取地址并输入url 例如:as http://autocache.abc.com/并输入,a文件将以 .pac 格式下载,保存到桌面

3>在textpad中打开.pac文件,识别PROXY:

在你的编辑器中,它会是这样的:

返回 "PROXY web-proxy.ind.abc.com:8080; PROXY proxy.sgp.abc.com:8080";

kudos to bekur from maven in 5 min not working

在 5 分钟内maven向 bekur 致敬不工作

Once you have the host and port just pop in into this and your good to go

一旦你拥有主机和端口,就可以进入这个,你就可以开始了

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("web-proxy.ind.abc.com", 8080));
        URLConnection connection = new URL(url).openConnection(proxy);

回答by Anton

For Java 1.8 and higher you must set -Djdk.http.auth.tunneling.disabledSchemes=to make proxies with Basic Authorization working with https.

对于 Java 1.8 及更高版本,您必须设置-Djdk.http.auth.tunneling.disabledSchemes=为使用 https 使用基本授权的代理。