Java Http串連中(HttpURLConnection)中使用代理(Proxy)及其驗證(Authentication)__Java

來源:互聯網
上載者:User

      使用Java的HttpURLConnection類可以實現HttpClient的功能,而不需要依賴任何其他類庫。所有有時候大家就直接使用它來完成一些簡單(或複雜)的功能。但是你活在偉大的{print G.F.W}後面,如果你需要訪問的網站被牆了,那HttpURLConnection類就會出現連線逾時的錯誤。這時候就需要給他設定代理(Proxy)了。

      設定代理(Proxy)可以有兩種方式:

      1、通過設定系統屬性(System.setPropery(String key, String value)的方式

      首先你可以在這裡看到Java支援的屬性。我們可以使用其中的http.proxyHost,http.proxyPort這兩個屬性。顧名思義,就是分別設定Proxy 伺服器地址和代理連接埠。

      //在你發起Http請求之前設定一下屬性 System.setProperty("http.proxyHost", "www.proxy.com"); System.setProperty("http.proxyPort", "8080");

      替換上面的www.proxy.com為你的Proxy 伺服器地址或IP地址,以及相應的連接埠為真實連接埠,Http串連及可以工作了。需要注意的是如果你設定了這些屬性,那麼所有的Http請求都會通過Proxy 伺服器。這些屬性是JVM層級的,設定了以後對所有的同類請求都有效。比如上面的是關於http的,還有關於ftp的等等。

      如果你的Proxy 伺服器不需要驗證,那到此就結束了。但一般都是需要驗證的。但是你要是看了上面Java支援的屬性列表,你就會發現那裡面並沒有期望中的

      http.proxyUserName=username http.proxyPassword=password 這兩個屬性。 這時就需要java.net.Authenticator類來完成一般的Http驗證。但是java.net.Authenticator這個類卻是個抽象類別,我們要使用還需要執行個體化一下子自己的類。個人覺得這裡很不方便。如下:

      public class BasicAuthenticator extends Authenticator { String userName; String password; public BasicAuthenticator(String userName, String password) { this.userName = userName; this.password = password; } /** * Called when password authorization is needed. Subclasses should * override the default implementation, which returns null. * * @return The PasswordAuthentication collected from the * user, or null if none is provided. */ @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password.toCharArray()); } }

       我們需要覆蓋java.net.Authenticator類的getPasswordAuthentication()方法,並返回一個PasswordAuthentication執行個體。要使他起作用,還需要設定

      Authenticator.setDefault(new BasicAuthenticator(userName, password));

      這樣就提供了基於Http Basic的驗證,接著就可以順暢的使用需要驗證的代理了。 

      2、通過java.net.Proxy類。

      這種方式是執行個體化一個Proxy類提供Proxy 伺服器的資訊,如連接埠和地址。

      Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port)); URLConnection conn = url.openConnection(proxy);

      使用代理的方式是在開啟Http串連的時候同時傳遞一個Proxy參數。如果需要驗證資訊的話我們可以添加一個Http頭參數來實現。

      //格式如下: "Proxy-Authorization"= "Basic Base64.encode(user:password)" String headerKey = "Proxy-Authorization"; String headerValue = "Basic " + Base64.encode(user+":"+password); conn.setRequestProperty(headerKey, headerValue); //..........

      其中的Base64.encode(user:password)是指把使用者名稱和密碼用冒號串連起來之後使用Base64編碼後的值作為值的一部分。  

      通過這種方式隻影響特定的Http串連,但是需要對代碼進行修改。這種方式下是否可以使用Authenticator還未做驗證。

    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.