Java System.setProperty代理

时间:2017-02-07 10:23:10

标签: java proxy

我使用它来验证HTTP URL以获取JSON字符串

        HttpURLConnection inputStream = (HttpURLConnection) myURL.openConnection();


        inputStream.setRequestProperty("Authorization", "Basic " + authStringEncoded);

我需要使用

System.setProperty("https.proxyHost", host);
        System.setProperty("https.proxyPort", port);

通过代理建立我的连接。现在我的新代理也需要验证。我只需要添加

是否正确
System.setRequestProperty("Authorization", "Basic " + authStringEncoded);

1 个答案:

答案 0 :(得分:1)

当您尝试使用https目标时,需要以下代码。您可以检查Authenticator,使其比下面提供的最小值更详尽。

Authenticator.setDefault(new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("user", "pwd".toCharArray());
        }
    });

如果您正在处理http链接,通过在请求中添加Proxy-Authorization标头,下面本身就足够了 -

inputStream.setRequestProperty("Proxy-Authorization", "Basic " + authStringEncoded);