簡體   English   中英

Java System.setProperty代理

[英]Java System.setProperty 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);

當您嘗試使用https目標時,將需要以下代碼。 您可以檢查Authenticator ,使其比下面提供的最小Authenticator更為詳盡。

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

如果您要處理的是http鏈接,則只需將Proxy-Authorization標頭添加到請求中,以下內容就足夠了-

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM