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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17183277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:13:47  来源:igfitidea点击:

HTTP Error 407 Proxy authentication required

javahttpproxy

提问by Arun

I am trying to access the url by using this code

我正在尝试使用此代码访问 url

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

This is working fine in my window machine but this is not working in linux machine. i am getting eror like this

这在我的 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)

线程“main”中的异常 java.io.IOException:服务器返回 HTTP 响应代码:URL 为 407:http: //www.google.com/at sun.net.www.protocol.http.HttpURLConnection.getInputStream(未知来源)在 com.yahoo.Connection.main(Connection.java:31)

Even proxy settings are correct and i tried like this also

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

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

But Same Error and i tried to set the export http_proxy= in /etc/profilebut no use

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

Any idea where it is going wrong.

知道哪里出错了。

回答by Rohit Dodle

I had the same problem. The following worked for me.

我有同样的问题。以下对我有用。

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

回答by Nu?ito de la Calzada

The following worked for me

以下对我有用

public class TEST4 {

    public static void main(String[] args) throws IOException {

        System.setProperty("http.proxyHost", "147.67.217.23");
        System.setProperty("http.proxyPort", "8012");
        URL url=new URL("http://stackoverflow.com");
        URLConnection uc = url.openConnection ();
        String encoded = new String (base64Encode(new String("pecador:d8kjk69t")));
        uc.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
        uc.connect();

        BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();

    }

    public static String userNamePasswordBase64(String username, String password) {
        return "Basic " + base64Encode(username + ":" + password);
    }

    private final static char base64Array[] = { 'A', 'B', 'C', 'D', 'E', 'F',
            'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
            'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
            'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
            't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', '+', '/' };

    private static String base64Encode(String string) {
        String encodedString = "";
        byte bytes[] = string.getBytes();
        int i = 0;
        int pad = 0;
        while (i < bytes.length) {
            byte b1 = bytes[i++];
            byte b2;
            byte b3;
            if (i >= bytes.length) {
                b2 = 0;
                b3 = 0;
                pad = 2;
            } else {
                b2 = bytes[i++];
                if (i >= bytes.length) {
                    b3 = 0;
                    pad = 1;
                } else
                    b3 = bytes[i++];
            }
            byte c1 = (byte) (b1 >> 2);
            byte c2 = (byte) (((b1 & 0x3) << 4) | (b2 >> 4));
            byte c3 = (byte) (((b2 & 0xf) << 2) | (b3 >> 6));
            byte c4 = (byte) (b3 & 0x3f);
            encodedString += base64Array[c1];
            encodedString += base64Array[c2];
            switch (pad) {
            case 0:
                encodedString += base64Array[c3];
                encodedString += base64Array[c4];
                break;
            case 1:
                encodedString += base64Array[c3];
                encodedString += "=";
                break;
            case 2:
                encodedString += "==";
                break;
            }
        }
        return encodedString;
    }

}