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

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

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

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

      首先你可以在這裏看到Java支持的屬性。我們可以使用其中的http.proxyHost,http.proxyPort這兩個屬性。顧名思義,就是分別設置代理服務器地址和代理端口。

     

[c-sharp] view plain copy
 print?
  1. //在你發起Http請求之前設置一下屬性  
  2. System.setProperty("http.proxySet", "true");
  3. System.setProperty("http.proxyHost""www.proxy.com");  
  4. System.setProperty("http.proxyPort""8080");  

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

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

      

[c-sharp] view plain copy
 print?
  1. http.proxyUserName=username  
  2. http.proxyPassword=password  
 這兩個屬性。 這時就需要java.net.Authenticator類來完成一般的Http驗證。但是java.net.Authenticator這個類卻是個抽象類,我們要使用還需要實例化一下子自己的類。個人覺得這裏很不方便。如下:

     

[java] view plain copy
 print?
  1. public class BasicAuthenticator extends Authenticator {  
  2.     String userName;  
  3.     String password;  
  4.   
  5.     public BasicAuthenticator(String userName, String password) {  
  6.         this.userName = userName;  
  7.         this.password = password;  
  8.     }  
  9.   
  10.     /** 
  11.      * Called when password authorization is needed.  Subclasses should 
  12.      * override the default implementation, which returns null. 
  13.      * 
  14.      * @return The PasswordAuthentication collected from the 
  15.      *         user, or null if none is provided. 
  16.      */  
  17.     @Override  
  18.     protected PasswordAuthentication getPasswordAuthentication() {  
  19.         return new PasswordAuthentication(userName, password.toCharArray());  
  20.     }  
  21. }  

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

     

[java] view plain copy
 print?
  1. Authenticator.setDefault(new BasicAuthenticator(userName, password));  

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

      2、通過java.net.Proxy類。

      這種方式是實例化一個Proxy類提供代理服務器的信息,如端口和地址。

     

[java] view plain copy
 print?
  1. Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));  
  2. URLConnection conn = url.openConnection(proxy);  

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

     

[java] view plain copy
 print?
  1. //格式如下:  
  2. "Proxy-Authorization""Basic Base64.encode(user:password)"  
  3. String headerKey = "Proxy-Authorization";  
  4. String headerValue = "Basic " + Base64.encode(user+":"+password);  
  5. conn.setRequestProperty(headerKey, headerValue);  
  6.   
  7. //..........  

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

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章