需要http 407代理身份validation:如何在java代码中处理

System.setProperty("http.proxySet", "true"); System.setProperty("java.net.useSystemProxies", "true"); System.setProperty("http.proxyHost", "192.168.1.103"); System.setProperty("http.proxyPort", "3128"); System.setProperty("http.proxyUser", "user123"); System.setProperty("http.proxyPassword", "passwD123"); url = new URL("http://www.google.co.in"); 

每当我使用此代码时,IOException抛出HTTP响应代码407.HTTP 407表示需要代理身份validation。 为什么在设置proxyUser和proxyPassword时会出现此问题。 在此处输入图像描述
如果我输错密码会出现http 401,但它总是给我407,意味着我的代码不带用户名和密码。 在上面的代码中,user123是用户名,passwD123是用于代理validation的密码。

http://blog.vinodsingh.com/2008/05/proxy-authentication-in-java.html

我找到了解决方案,感谢Vinod Singh先生。

Java中的代理身份validation

通常的企业网络通过代理服务器提供互联网访问,有时它们也需要身份validation。 应用程序可以打开与企业内部网外部服务器的连接。 因此,必须以编程方式进行代理身份validation。 幸运的是,Java提供了一种透明的机制来进行代理身份validation。

创建一个简单的类,如下所示 –

 import java.net.Authenticator; class ProxyAuthenticator extends Authenticator { private String user, password; public ProxyAuthenticator(String user, String password) { this.user = user; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password.toCharArray()); } } 

并在代码打开URLConnection之前放置这些代码行 –

 Authenticator.setDefault(new ProxyAuthenticator("user", "password")); System.setProperty("http.proxyHost", "proxy host"); System.setProperty("http.proxyPort", "port"); 

现在所有呼叫都将成功通过代理身份validation。

@GauravDS你提到过:

http://blog.vinodsingh.com/2008/05/proxy-authentication-in-java.html我找到了解决方案,感谢Vinod Singh先生。 Java中的代理身份validation通常的企业网络通过代理服务器提供Internet访问,有时也需要身份validation。 应用程序可以打开与企业内部网外部服务器的连接。 因此,必须以编程方式进行代理身份validation。 幸运的是,Java提供了一种透明的机制来进行代理身份validation。 创建一个简单的类,如下所示。


并在代码打开URLConnection- Authenticator.setDefault(new ProxyAuthenticator("user", "password")); System.setProperty("http.proxyHost", "proxy host"); System.setProperty("http.proxyPort", "port");之前放置这些代码行Authenticator.setDefault(new ProxyAuthenticator("user", "password")); System.setProperty("http.proxyHost", "proxy host"); System.setProperty("http.proxyPort", "port"); Authenticator.setDefault(new ProxyAuthenticator("user", "password")); System.setProperty("http.proxyHost", "proxy host"); System.setProperty("http.proxyPort", "port"); 现在所有呼叫都将成功通过代理身份validation。

如果您要连接的站点还需要用户名/密码,那该怎么办? 设置默认身份validation器(Authenticator.setDefault)将失败我想当外部站点将查找经过身份validation的用户时。

有什么意见吗?….有人吗?

编辑:1之前使用此代码并收到错误(407)需要代理身份validation。 我认为这是因为身份validation是由不同的主机请求的。 当您为一个主机设置一个用户/通行证的默认validation器时,其他请求主机的validation将失败。 我昨天对SimpleAuthenticator类进行了以下更改,现在它就像一个魅力。

  protected PasswordAuthentication getPasswordAuthentication() { String requestingHost = getRequestingHost(); if (requestingHost == proxyHost){ System.out.println("getPasswordAuthentication() request recieved from->" + requestingHost ); return new PasswordAuthentication(proxyuser,proxypass.toCharArray()); } else{ System.out.println("getPasswordAuthentication() request recieved from->" + requestingHost ); return new PasswordAuthentication(sharepointusername,sharepointpassword.toCharArray()); } } 

更多信息: http : //blog.ashwani.co.in/blog/2013-07-29/access-sharepoint-webservices-from-java-behind-proxy/

使用Authenticator的答案对于一般情况是正确的。 但是, Java 8u111及更高版本中HTTP 407的另一个原因是,如果您对代理使用BASIC身份validation。

在这种情况下,添加此系统属性:

 -Djdk.http.auth.tunneling.disabledSchemes= 

我发现了这个: https : //confluence.atlassian.com/kb/basic-authentication-fails-for-outgoing-proxy-in-java-8u111-909643110.html