java - HTTP 错误 407 需要代理身份验证

标签 java http proxy

我正在尝试使用此代码访问该网址

System.setProperty("http.proxyHost", "111.88.15.108");
    System.setProperty("http.proxyPort", "8002");
    System.setProperty("http.proxyUser", "user");
    System.setProperty("http.proxyPassword", "password");
    URL oracle = new URL("http://www.google.com/");
    URLConnection yc = oracle.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(
            yc.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null)

        System.out.println(inputLine);
    in.close();

这在我的 Windows 机器上工作正常,但在 Linux 机器上不起作用。我遇到这样的错误

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 407 for URL: http://www.google.com/ at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at com.yahoo.Connection.main(Connection.java:31)

即使代理设置也是正确的,我也尝试过这样

java -Dhttp.proxyHost="111.88.15.108" -Dhttp.proxyPort="8002" -Dhttp.proxyUser="user" -Dhttp.proxyPassword="password"  -jar yahoo_test3.jar

但是同样的错误,我尝试在/etc/profile 中设置 export http_proxy= 但没有用

知道哪里出了问题。

最佳答案

我也遇到了同样的问题。以下内容对我有用。

Authenticator.setDefault(new Authenticator() 
{
    protected PasswordAuthentication getPasswordAuthentication() 
    {
    return new PasswordAuthentication("username","password".toCharArray());
    }
});

关于java - HTTP 错误 407 需要代理身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17183277/

相关文章:

java - app-context.xml 的完整 Java 配置,包括 jdbc 和 tx 指令等

java - 无法将 "POST"请求设置为 HttpURLConnection 对象

css - Respond.js 跨域代理不工作

php - 如何将 PHP 脚本变成代理服务器?

node.js - Nginx 和 Node.js - 充分利用服务器

java - 当我取消注释一个简单方法时应用程序崩溃

java - 随机像素的图像

java - 如何在 java 中对一系列整数进行单元测试?

java - 限制 Tomcat 中的最大请求大小

javascript - Angular 2如何从innerHTML执行脚本标签