2017-01-06 42 views
4

我面对的Java6/8的好奇行为。 我尝试通过需要基本用户身份验证的代理进行隧道传输。 这是通过标准的java Authenticator来完成的。 如果我尝试访问https网址作为第一个url,则抛出异常:无法通过代理进行隧道传输。代理返回“HTTP/1.1 407”通过https

java.io.IOException:无法通过代理进行隧道传输。代理返回“HTTP/1.1 407代理服务器身份验证”

但是,如果我先访问HTTP URL的HTTPS URL,HTTPS访问工作正常。

鉴于代码:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.Authenticator; 
import java.net.HttpURLConnection; 
import java.net.InetSocketAddress; 
import java.net.PasswordAuthentication; 
import java.net.Proxy; 
import java.net.URL; 

public class ProxyPass { 
    public ProxyPass(String proxyHost, int proxyPort, final String userid, final String password, String url) { 

    try { 
      /* Create a HttpURLConnection Object and set the properties */ 
      URL u = new URL(url); 
      Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); 
      HttpURLConnection uc = (HttpURLConnection) u.openConnection(proxy); 

      Authenticator.setDefault(new Authenticator() { 
       @Override 
       protected PasswordAuthentication getPasswordAuthentication() { 
        if (getRequestorType().equals(RequestorType.PROXY)) { 
         return new PasswordAuthentication(userid, password.toCharArray()); 
        } 
        return super.getPasswordAuthentication(); 
       } 
      }); 
      uc.connect(); 
      /* Print the content of the url to the console. */ 
      showContent(uc); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private void showContent(HttpURLConnection uc) throws IOException { 
     InputStream i = uc.getInputStream(); 
     char c; 
     InputStreamReader isr = new InputStreamReader(i); 
     BufferedReader br = new BufferedReader(isr); 
     String line; 
     while ((line = br.readLine()) != null) { 
      System.out.println(line); 
     } 
    } 

    public static void main(String[] args) { 
     String proxyhost = "proxyHost"; 
     int proxyport = proxyPort; 
     final String proxylogin = proxyUser; 
     final String proxypass = proxyPass; 

     String url = "http://www.google.de"; 
     String surl = "https://www.google.de"; 

//   new ProxyPass(proxyhost, proxyport, proxylogin, proxypass, url); // uncomment this line to see that the https request works! 
//   System.out.println(url + " ...ok"); // uncomment this line to see that the https request works! 
     new ProxyPass(proxyhost, proxyport, proxylogin, proxypass, surl); 
     System.out.println(surl + " ...ok"); 
    } 

任何建议,想法?

回答

5

你必须编辑变量 jdk.http.auth.tunneling.disabledSchemesjdk.http.auth.proxying.disabledSchemes空白像这样:
jdk.http.auth.tunneling.disabledSchemes =
jdk.http.auth.proxying.disabledSchemes =

在我来说,我在这个文件中找到

jdk1.8.0_111/JRE/lib目录/ net.properties

+0

因此,您必须在每次JDK更新后执行此操作? – mbee

19

变化的Java 8更新111:

现在,需要基本身份验证设置时,隧道 用于HTTPS将不再默认情况下成功代理。如果需要,可以通过从 jdk.http.auth.tunneling.disabledSchemes网络属性中删除基本,或者通过 在 命令行上将相同名称的系统属性设置为“”(空)来重新激活此 认证方案。

http://www.oracle.com/technetwork/java/javase/8u111-relnotes-3124969.html

你可以尝试用

java -Djdk.http.auth.tunneling.disabledSchemes="" 

或可能是更好的(安全)运行Java:升级你的代理,如认证方案摘要访问认证。

+0

设置了该选项,现在工作的建议。非常感谢这个提示! – PPr