0

我正在尝试使用此代码访问 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();

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

线程“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)

甚至代理设置都是正确的,我也尝试过这样

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

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

任何想法哪里出错了。

4

2 回答 2

9

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

Authenticator.setDefault(new Authenticator() 
{
    protected PasswordAuthentication getPasswordAuthentication() 
    {
    return new PasswordAuthentication("username","password".toCharArray());
    }
});
于 2014-05-17T17:17:11.183 回答
0

以下对我有用

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;
    }

}
于 2015-06-23T14:19:23.630 回答