'Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authorization Required"

I was trying to hit the https url for google api. Using the code below but its giving some errors. but i can hitting one google api http url and its working very well without any error

DataInputStream di = null;

FileOutputStream fo = null;

byte[] b = new byte[1];

// PROXY
System.setProperty("https.proxyHost", "my proxy");
System.setProperty("https.proxyPort", "8080");


Authenticator.setDefault(new PasswordAuthenticator());


URL u = new URL("https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode&language=fr&sensor=true&key=keyforuse");

HttpURLConnection con = (HttpURLConnection) u.openConnection();

sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
String encodedUserPwd = encoder.encode("domain\\user:password"
        .getBytes());
con.setRequestProperty("Proxy-Authorization", "Basic "
        + encodedUserPwd);

di = new DataInputStream(con.getInputStream());
// result = parseJSON(di);
while (-1 != di.read(b, 0, 1)) {
    System.out.print(new String(b));
}

but getting error below

java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authorization Required"
    at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:1648)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:164)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
    at kf.store.locator.googlemaps.test.main(test.java:58)

any help????



Solution 1:[1]

You may not need the domain in front of the username. I had a very similar issue, and I fixed it when I removed the domain in front of the username. Since the proxy server is on a domain, I think it implies that your under the same domain.

Solution 2:[2]

I fixed it by authenticating to the proxy server with my user name and password. If you are on your company's network , it can be your sso cred too. The equivalent of this will be like this :

-Dhttp.proxyUser=myusername 
-Dhttp.proxyPassword=mypassword
-Dhttp.proxyHost=myproxyserver.com
-Dhttp.proxyPort=9000

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Callan
Solution 2 Ishu Gupta