161

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.

izb
  • 50,101
  • 39
  • 117
  • 168

7 Answers7

382

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);
Will
  • 14,348
  • 1
  • 42
  • 44
NickDK
  • 5,159
  • 2
  • 18
  • 11
  • 7
    can we provide proxy username and proxy password through it. – Xolve Jul 04 '10 at 10:15
  • 1
    Exactly what I was looking. And yet I accidentally clicked downvote while copy/pasting, only to notice too late to undo it. Sorry. – Chris Noe Jun 06 '13 at 20:59
  • 20
    What if you have different username/password pairs for the different proxies? Calling a static method to set the default Authenticator isn't ideal, this is not much better than setting the sys properties method.. – javaPhobic Apr 30 '15 at 00:56
  • I'm trying to use this to check connection with google.com but `getResponseCode()` returns `recvfrom failed: ECONNRESET (Connection reset by peer)` exception. ¿what would be wrong? – Hanzo Apr 28 '16 at 09:19
  • 1
    Do we have to call Authenticator every time we open a HttpURLConnection? Or set this only once? – cecemel Jul 05 '16 at 16:02
  • 2
    Authenticator.default is a static (i.e. global) variable, so it's only once. But please note that the Authenticator above is just a minimal example. It can only handle one password at a time. Google for examples that can handle multiple hosts with different passwords. – Stroboskop Nov 14 '16 at 14:48
  • 4
    Since 8u11 this will not work by default with Basic authentication, http://www.oracle.com/technetwork/java/javase/8u111-relnotes-3124969.html jdk.http.auth.tunneling.disabledSchemes system property must be set to emtpty – white Dec 20 '16 at 10:43
  • 3
    In case you have domain. Do as following: `new PasswordAuthentication("domainName\\user", "password".toCharArray());` – Developer Marius Žilėnas Feb 15 '17 at 08:20
  • @NickDK, Your answer is very good and easy to understand, I was looking for the same piece of code, but I am getting " java.net.SocketTimeoutException: connect timed out " while establishing the connecting. Can someone please guide me – Pradeep hebbar Aug 02 '19 at 16:49
  • 2
    for multiple proxies, instead of using an Authenticator like in the example, just setting the 'Proxy-Authorization' header is enough, but may not always fit your requirements, for details see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authorization – Chris Aug 21 '19 at 13:52
  • @Chris, I would like to clarify your statement: "just setting the 'Proxy-Authorization'" & , "but may not always fit your requirements". If proxy requires NTLM authentication other than Basic authentication, setting 'Proxy-Authorization' will not be as simple as in case of Basic auth. – garnet Feb 26 '20 at 13:48
  • The `Authenticator` part resolved the issue for me using the okhttp3 library as well using a SOCKS5 proxy. – blacktide Aug 16 '20 at 03:44
  • How to read cookie from target and write to response for user browser? – MrSalesi Oct 07 '22 at 20:21
38

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.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • 1
    Please edit your answer to include for the scenario when it's https. If you connect to a https endpoint you have to use **https.proxyHost** and **https.proxyPort**. – Cristian Balint Sep 03 '21 at 09:03
  • Although your answer seems to be correct, it lacks details. Please add some code examples as to how to use these properties. – avi.elkharrat Aug 02 '22 at 09:30
23

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);
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • @Pascal Do you happen to know what are the major differences of using latest Java approach in comparison to Apache `commons-httpclient`? As Java supports proxying and authentication (as you mentioned here http://stackoverflow.com/questions/1626549/authenticated-http-proxy-with-java), for simple cases (like retrieve one file from public HTTP server) there is no reason to use Apache library. What is your recommendation? – dma_k Mar 04 '10 at 20:18
  • @dma_k I agree with you, for simple use cases like the one you described I wouldn't use a third party library. – Pascal Thivent Mar 04 '10 at 21:02
  • Do you know how to support the nonProxyHosts? I see that my device support it but doesn't know how to make my app handle it. – RiRomain Nov 11 '16 at 09:58
  • But variable `systemProperties` is not used by the `connection`! – parsecer Mar 30 '19 at 02:59
  • How to pass read cookie ??? – MrSalesi Oct 07 '22 at 20:18
19

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
    This works only with manual proxy server configuration. Automatic proxy configuration and proxies configured through script are not (yet) propagated to "useSystemProxies". – Tires Jun 24 '14 at 08:48
  • 2
    This worked for me when setting the proxyHost and proxyPort didn't. Thanks! – nrobey Apr 02 '15 at 18:53
  • Likewise, this worked from behind my company proxy when calls to `System.setProperty` for the `https.proxyHost` and `https.proxyPort` for some reason weren't cutting the mustard. – Pavel Komarov Oct 18 '19 at 14:18
10

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");
ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
  • 7
    I actually think "http.proxyUser" and "http.proxyPassword" are not supported anymore. See http://stackoverflow.com/questions/120797/how-do-i-set-the-proxy-to-be-used-by-the-jvm for more details. – p3t0r Oct 29 '09 at 20:19
6

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

Anton
  • 5,831
  • 3
  • 35
  • 45
  • 1
    Background information about this is discussed at https://stackoverflow.com/questions/41806422/java-web-start-unable-to-tunnel-through-proxy-since-java-8-update-111 – U880D Mar 09 '18 at 07:50
4

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);
Community
  • 1
  • 1
Norbert
  • 809
  • 9
  • 13