2012-04-12 30 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中的处理依赖关系。