Reputation: 2496
I have a problem with HttpsURLConnection
- proxy is not used.
Here is the code:
//proxy
String type = "https";
System.getProperties().put(type + ".proxyHost", host);
System.getProperties().put(type + ".proxyPort", port);
System.getProperties().put(type + ".proxyUser", username);
System.getProperties().put(type + ".proxyPassword", password);
/*some SSL stuff*/
//connection
URL url = new URL(url0);
URLConnection urlConnection = url.openConnection();
urlConnection.setUseCaches(false);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(false);
urlConnection.setRequestProperty("Connection", "Keep-Alive");
HttpsURLConnection httpConn = (HttpsURLConnection)urlConnection;
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestProperty("Proxy-Authorization", "Basic " + Base64Converter.encode(username + ":" + password));
httpConn.connect();
All proxy settings are ignored by the connection and httpConn.usingProxy()
is false
.
I also tried to pass Proxy
instance to url.openConnection()
and setting proxy login/password to default Authenticator
. The connection used proxy in that case, but I got 407, so it seems that Authenticator doesn't work correctly for me.
Upvotes: 1
Views: 7643
Reputation: 310903
System.getProperties().put(type + ".proxyUser", username);
System.getProperties().put(type + ".proxyPassword", password);
According to the official documentation, the JRE does not recognized either of these. I believe the Apache HTTP Client may do so, but don't quote me.
You need to install a java.net.Authenticator.
Upvotes: 1
Reputation: 2167
From How do I make HttpURLConnection use a proxy?:
Since java 1.5 you can also pass a java.net.Proxy instance to the openConnection() 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);
Upvotes: 2