2013-01-01 50 views
10
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意味着需要代理认证。为什么在我设置proxyUser和proxyPassword时出现此问题。 enter image description here
如果我输入了错误的密码,会发生http 401,但它总是给我407,这意味着我的代码不需要用户名和密码。在上面的代码中user123是用户名,passwD123是代理认证的密码。HTTP 407代理认证:如何在Java代码中

+1

的HTTP客户端您使用的? – Cratylus

+0

我正在尝试在java中制作手动代理,自动代理,无代理支持浏览器。像Firefox这样的浏览器支持这个,工具 - >选项 - >高级 - >网络 - >设置。 – dayitv89

回答

19

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

我找到了解决办法感谢维诺德·辛格先生。在Java中

代理身份验证通常的企业网络提供了通过代理服务器上网,有时他们需要进行身份验证也是如此。可能的应用程序确实打开了与企业内部网络外部的服务器的连接。所以必须以编程方式进行代理身份验证。幸运的是,Java提供了一种透明的机制来进行代理认证。

创建一个简单的类象如下─

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"); 

之前,现在,所有的呼叫都将顺利通过代理认证。

+2

此代码用于手动代理支持。 – dayitv89

+0

我在eclipse上用用户名和密码在运行配置中设置了http和https代理,由于某些原因它间歇性地工作,这很奇怪。 –

7

@GauravDS 你提到:

http://blog.vinodsingh.com/2008/05/proxy-authentication-in-java.html 我找到了解决办法感谢维诺德·辛格先生。 Java中的代理认证 通常的公司网络通过代理服务器提供互联网访问,有时他们也需要认证。可能的应用程序确实打开了与企业内部网络外部的服务器的连接。所以必须以编程方式进行代理身份验证。幸运的是,Java提供了一种透明的机制来进行代理认证。 创建一个简单的类如下 - 。


并且在代码打开之前放上这些代码行URLConnection- Authenticator.setDefault(new ProxyAuthenticator("user", "password")); System.setProperty("http.proxyHost", "proxy host"); System.setProperty("http.proxyPort", "port"); 现在所有的调用都会成功通过代理验证。

如果您要连接到的网站还需要用户名/密码才能允许,该怎么办? 设置一个默认的身份验证器(Authenticator.setDefault)将失败我猜当外部网站将寻找经过身份验证的用户。

任何意见?....有人?

编辑:1 以前使用此代码,并得到错误(407)代理身份验证要求。 我认为这是因为身份验证是由不同主机请求的。当您为一台主机设置一个用户/密码的默认身份验证器时,则其他请求主机的身份验证将失败。我昨天做了以下更改为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/

+0

什么http响应为站点身份验证? 以上公司/研究所网络中代理服务器的机制。 – dayitv89

+0

上面编辑了我的回复。 – TheAshwaniK

+1

您也可以检查'getRequestorType()== RequestorType.PROXY'。身份验证对于信任或登录应用程序通常使用401和代理使用407. –