title: Basic proxy authentication for HTTPS URLs returns HTTP/1.0 407 Proxy Authentication Required
body: <p>I want to use a proxy with basic authentication (username, password) for a connection (and only this connection) in Java. The following code works for HTTP URLs (e.g. "<a href="http://www.google.com" rel="noreferrer">http://www.google.com</a>"):</p>
  
  <pre><code>URL url = new URL("http://www.google.com");
  HttpURLConnection httpURLConnection = null;
  InetSocketAddress proxyLocation = new InetSocketAddress(proxyHost, proxyPort);
  Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyLocation);
  httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
  // Works for HTTP only! Doesn't work for HTTPS!
  String encoded = new sun.misc.BASE64Encoder().encodeBuffer((proxyUserName + ":" + proxyPassword).getBytes()).replace("\r\n", "");
  httpURLConnection.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
  InputStream is = httpURLConnection.getInputStream();
  InputStreamReader isr = new InputStreamReader(is); 
  int data = isr.read();
  while(data != -1){
    char c = (char) data;
    data = isr.read();
    System.out.print(c);
  }
  isr.close();
  </code></pre>
  
  <p>The code doesn't work for HTTPS URLs (e.g. "<a href="https://www.google.com" rel="noreferrer">https://www.google.com</a>"), though! I get <code>java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.0 407 Proxy Authentication Required"</code> when I try to access an HTTPS URL.</p>
  
  <p>This code works for HTTP and HTTPS:</p>
  
  <pre><code>URL url = new URL("https://www.google.com");
  HttpURLConnection httpURLConnection = null;
  InetSocketAddress proxyLocation = new InetSocketAddress(proxyHost, proxyPort);
  Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyLocation);
  httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
  // Works for HTTP and HTTPS, but sets a global default!
  Authenticator.setDefault(new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(proxyUserName, proxyPassword.toCharArray());
    }
  });
  InputStream is = httpURLConnection.getInputStream();
  InputStreamReader isr = new InputStreamReader(is); 
  int data = isr.read();
  while(data != -1){
    char c = (char) data;
    data = isr.read();
    System.out.print(c);
  }
  isr.close();
  </code></pre>
  
  <p>The problem with the 2nd code is that it sets a new default <code>Authenticator</code> and I don't want to do that, because this proxy is only used by a part of the application and a different part of the application could be using a different proxy. I don't want to set a global default for the whole application. Is there a way to get the 1st code to work with HTTPS or a way to use an <code>Authenticator</code> without setting it as default?</p>
  
  <p>I have to use <code>java.net.HttpURLConnection</code>, because I'm overriding a method of a class which has to return an <code>HttpURLConnection</code>, so I can't use Apache HttpClient.</p>