A DESCRIPTION OF THE PROBLEM :
I am trying to connect to a web server that uses https through a proxy which requires authentication, but it doesn't work.
The HttpsURLConnection class is sending the first request to open the tunnel without the Proxy-Authorization header even if forced.
In this case the server will return "407 Proxy Authentication Required".
I think that this behavior is caused by the method sun.net.www.protocol.http.HttpURLConnection.sendCONNECTRequest()
It creates a new connect request without adding the Proxy-Authorization header.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
public class TestProxy {
@Test
public void testOne() throws IOException {
final String PROXY_USERNAME = "username";
final String PROXY_PASSWORD = "password";
final String PROXY_HOSTNAME = "proxyhostname";
final int PROXY_PORT = 8080;
Authenticator.setDefault(
new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(PROXY_USERNAME, PROXY_PASSWORD.toCharArray());
}
});
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOSTNAME, PROXY_PORT));
URL url = new URL("https://www.google.com");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(proxy);
assertNotEquals(407, connection.getResponseCode());
}
@Test
public void testTwo() throws IOException {
final String PROXY_USERNAME = "username";
final String PROXY_PASSWORD = "password";
final String PROXY_HOSTNAME = "proxyhostname";
final int PROXY_PORT = 8080;
Authenticator.setDefault(
new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(PROXY_USERNAME, PROXY_PASSWORD.toCharArray());
}
});
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOSTNAME, PROXY_PORT));
URL url = new URL("https://www.google.com");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(proxy);
String userCredentials = PROXY_USERNAME + ":" + PROXY_PASSWORD;
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
connection.setRequestProperty("Proxy-Authorization", basicAuth);
assertNotEquals(407, connection.getResponseCode());
}
}
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
200 OK
ACTUAL -
407 Proxy Authentication Required
---------- BEGIN SOURCE ----------
I think that this behavior is caused by the method sun.net.www.protocol.http.HttpURLConnection.sendCONNECTRequest()
It creates a new « connect request » without adding the « Proxy-Authorization » header.
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
The workaround is to connect to a http address at first so the proxy server will register the user agent and authorize connecting without adding Proxy-Authorization header
FREQUENCY : always
I am trying to connect to a web server that uses https through a proxy which requires authentication, but it doesn't work.
The HttpsURLConnection class is sending the first request to open the tunnel without the Proxy-Authorization header even if forced.
In this case the server will return "407 Proxy Authentication Required".
I think that this behavior is caused by the method sun.net.www.protocol.http.HttpURLConnection.sendCONNECTRequest()
It creates a new connect request without adding the Proxy-Authorization header.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
public class TestProxy {
@Test
public void testOne() throws IOException {
final String PROXY_USERNAME = "username";
final String PROXY_PASSWORD = "password";
final String PROXY_HOSTNAME = "proxyhostname";
final int PROXY_PORT = 8080;
Authenticator.setDefault(
new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(PROXY_USERNAME, PROXY_PASSWORD.toCharArray());
}
});
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOSTNAME, PROXY_PORT));
URL url = new URL("https://www.google.com");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(proxy);
assertNotEquals(407, connection.getResponseCode());
}
@Test
public void testTwo() throws IOException {
final String PROXY_USERNAME = "username";
final String PROXY_PASSWORD = "password";
final String PROXY_HOSTNAME = "proxyhostname";
final int PROXY_PORT = 8080;
Authenticator.setDefault(
new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(PROXY_USERNAME, PROXY_PASSWORD.toCharArray());
}
});
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOSTNAME, PROXY_PORT));
URL url = new URL("https://www.google.com");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(proxy);
String userCredentials = PROXY_USERNAME + ":" + PROXY_PASSWORD;
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
connection.setRequestProperty("Proxy-Authorization", basicAuth);
assertNotEquals(407, connection.getResponseCode());
}
}
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
200 OK
ACTUAL -
407 Proxy Authentication Required
---------- BEGIN SOURCE ----------
I think that this behavior is caused by the method sun.net.www.protocol.http.HttpURLConnection.sendCONNECTRequest()
It creates a new « connect request » without adding the « Proxy-Authorization » header.
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
The workaround is to connect to a http address at first so the proxy server will register the user agent and authorize connecting without adding Proxy-Authorization header
FREQUENCY : always