从httpclient 3转换为httpclient 4(Cookie策略)

时间:2012-06-20 15:29:18

标签: java android apache-httpclient-4.x apache-commons-httpclient

以前,我在httpclient3

中有以下代码
// To prevent cookie warnings.
httpClient.getParams().setParameter("http.protocol.single-cookie-header", true);
httpClient.getParams().setCookiePolicy(org.apache.commons.httpclient.cookie.CookiePolicy.BROWSER_COMPATIBILITY);

我想知道,httpclient4中的等效代码是什么,因为我在第二行收到错误。

// To prevent cookie warnings.
httpClient.getParams().setParameter("http.protocol.single-cookie-header", true);
// ??? compilation error in this line.
// org.apache.commons.httpclient cannot be resolved to a variable
httpClient.getParams().setCookiePolicy(org.apache.commons.httpclient.cookie.CookiePolicy.BROWSER_COMPATIBILITY);

2 个答案:

答案 0 :(得分:3)

此链接可以帮助您解决问题。

http://www.wirelust.com/2009/03/29/howto-set-the-cookie-policy-with-apache-http-client-40/

所以试试这一行:

httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

答案 1 :(得分:0)

由于版本4.3 getParams()ClientPNamesCookiePolicy都已弃用。为了达到相同的效果,你必须这样做:

final RequestConfig params = RequestConfig
        .custom()
        .setCookieSpec(CookieSpecs.IGNORE_COOKIES)
        .build();

httpPost.setConfig(params);