6

I am trying to get contents of an URL through an authenticated proxy. This is the code i am trying to use:

        Authenticator authenticator = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                System.out.println("authenticating");
                return (new PasswordAuthentication("username", "password".toCharArray()));
            }
        };
        Authenticator.setDefault(authenticator);
        URL url = new URL("http://www.google.com");
        InetSocketAddress proxyAddress = new InetSocketAddress("address.of.proxy", 6060);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
        HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
        uc.connect();
        System.out.println(uc.getResponseCode());

For some reason, the authentication gets into a redirect loop, so the result is the authenticator printing "authenticating" 20 times, then a ProtocolException

java.net.ProtocolException: Server redirected too many  times (20)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1846)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at java.net.URLConnection.getContent(URLConnection.java:739)
at proxytest.RunThis.main(RunThis.java:29)

The proxy is working with the given credentials, i have tried it through browser. I am trying to get this working for days, i have tried setting system properties, apache httpclient, and anything i could get out of google. Any ideas appreciated. :)

UPDATE:

I tested with WireShark, the proxy authentication details are in the request, but the proxy throws back a 407 error. Again, the credentials are OK, its working perfectly from browser ( i actually copied them from the source code to make sure ).

There is one thing i noticed though. The value of the Proxy-Authorization header differs in one and only one character between the browser and the request sent by java. Can this mean something?

Koocka
  • 111
  • 1
  • 3
  • 9
  • i am having exactly the same problem...do you have an solution or any new ideas? – Ben Jan 08 '16 at 12:41
  • Further: I read about using an Cookie-Hander, but even with an enabled default cookie-handler `CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));` i get the same result... – Ben Jan 08 '16 at 12:58
  • @Koocka Hi, have you made it working? – Inoy Apr 21 '16 at 18:58
  • I'm having the same problem.. – dinhokz Oct 10 '16 at 22:51

2 Answers2

1

The Authenticator.setDefault(authenticator); must be after the openConnection(proxy);

URL url = new URL("http://www.google.com");
InetSocketAddress proxyAddress = new InetSocketAddress("address.of.proxy", 6060);
Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
    Authenticator.setDefault(new Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
            return (new PasswordAuthentication("username", "password".toCharArray()));
          }
        });
uc.connect();
System.out.println(uc.getResponseCode());
DuDa
  • 3,718
  • 4
  • 16
  • 36
Andres Robles
  • 125
  • 1
  • 9
0

Just set JVM property:

jdk.http.auth.tunneling.disabledSchemes = ""

See: http://www.oracle.com/technetwork/java/javase/8u111-relnotes-3124969.html

Leave it here because for an hour I researched this issue.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315