1

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.

4
  • What is the value of url0? Does it actually use https:// ? Commented Sep 2, 2013 at 7:40
  • Yes, it is "https://...." Commented Sep 2, 2013 at 8:50
  • Are you running the code on Windows - or which o/s does it run on? Commented Sep 2, 2013 at 9:31
  • Yes, I run it on Windows. Commented Sep 2, 2013 at 9:32

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I mentioned in my question that there are some problems with that method.
1
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.

1 Comment

Yep, it seems that I should mess with Authenticator to solve this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.