How do I make HttpURLConnection use a proxy?

JavaWindowsHttpProxy

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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. EDIT: per comment, set https.proxyPort and https.proxyHost for HTTPS.

Solution 3 - Java

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

Solution 4 - Java

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

Solution 5 - Java

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

Solution 6 - Java

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

Solution 7 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionizbView Question on Stackoverflow
Solution 1 - JavaNickDKView Answer on Stackoverflow
Solution 2 - JavaSean OwenView Answer on Stackoverflow
Solution 3 - JavaPascal ThiventView Answer on Stackoverflow
Solution 4 - JavaDaniel Worthington-BodartView Answer on Stackoverflow
Solution 5 - JavaZZ CoderView Answer on Stackoverflow
Solution 6 - JavaAntonView Answer on Stackoverflow
Solution 7 - JavaNorbertView Answer on Stackoverflow