How do I make HttpURLConnection use a proxy?

2019-01-01 03:34发布

问题:

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.

回答1:

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

//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.

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


回答2:

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



回答3:

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:

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


回答4:

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


回答5:

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)

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



回答6:

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



回答7:

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

kudos to bekur from maven in 5 min not working

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