2012-04-12 28 views
0

我在哪裏可以找到並下載api來實現那段代碼?我用谷歌搜索它,但我找不到它。我發現的唯一一個是:http://hc.apache.org/downloads.cgi但事實並非如此。謝謝。java項目的httpclient api

import org.apache.commons.httpclient.Cookie; 
import org.apache.commons.httpclient.HttpState; 
import org.apache.commons.httpclient.HttpClient; 
import org.apache.commons.httpclient.methods.GetMethod; 

public class GetCookiePrintAndSetValue { 

    public static void main(String args[]) throws Exception { 

    HttpClient client = new HttpClient(); 
    client.getParams().setParameter("j_username", "abc"); 
    client.getParams().setParameter("j_password", "pqr"); 

    GetMethod method = new GetMethod("http://localhost:8080/"); 
    try{ 
     client.executeMethod(method); 
     Cookie[] cookies = client.getState().getCookies(); 
     for (int i = 0; i < cookies.length; i++) { 
     Cookie cookie = cookies[i]; 
     System.err.println( 
      "Cookie: " + cookie.getName() + 
      ", Value: " + cookie.getValue() + 
      ", IsPersistent?: " + cookie.isPersistent() + 
      ", Expiry Date: " + cookie.getExpiryDate() + 
      ", Comment: " + cookie.getComment()); 
     } 
     client.executeMethod(method); 
    } catch(Exception e) { 
     System.err.println(e); 
    } finally { 
     method.releaseConnection(); 
    } 
    } 
} 
+1

你試過了什麼版本的雅加達Http客戶端? – dash1e 2012-04-12 17:57:18

+1

此代碼片段使用舊版本的apache庫。 – Carlo 2012-04-12 17:58:42

回答

5

按照project page

共同性HttpClient的項目現在結束生命,而不再被開發。在它的HttpClient和HttpCore模塊中它已被Apache HttpComponents project取代,它們提供更好的性能和更大的靈活性。

所以,你可能想要that page

如果你想要一箇舊版本,如3.0,你可以找到它in the archives

此外,我強烈建議學習使用Maven,因爲它會使項目much easier中的處理依賴關係。