4

以前,我在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);
4

2 回答 2

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);
于 2012-06-20T16:49:09.737 回答
1

从 4.3 版开始,getParams()全部弃用。为了获得相同的结果,您必须这样做:ClientPNamesCookiePolicy

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

httpPost.setConfig(params);
于 2017-03-22T19:53:48.533 回答