HttpClient警告:Cookie被拒绝:非法域属性
我正在使用HttpClient最新版本(4.x)。 而现在我正在尝试做一个GET请求。 我只是发布了一个Get请求。
这是我的代码;
public class Poster { static boolean routing1 = true, routing2 = true; static int counter1 = 0, counter2 = 0; DefaultHttpClient oHtp = null; HttpGet oHGet = null; HttpResponse oHRes = null; private void test(String fullAddress) throws Exception { oHtp = new DefaultHttpClient(); oHGet = new HttpGet(fullAddress); HttpResponse response = oHtp.execute(oHGet); System.out.print(response.getStatusLine()); HttpEntity entity = response.getEntity(); if (entity != null) { entity = new BufferedHttpEntity(entity); // System.out.println(EntityUtils.toString(entity)); System.out.print("\t entity is retrieved... "); } oHtp.getConnectionManager().shutdown(); } }
我只是很好地执行它。 首先是
new Poster().test("http://123.xl.co.id/profile.php");
第二是
new Poster().test("http://goklik.co.id/");
雅,只有第二个….我得到了这个错误信息;
2011年9月18日上午10:11:30 org.apache.http.client.protocol.ResponseProcessCookies processCookies警告:Cookie被拒绝:“[version:0] [name:CookiePst] [value:0149 = xwGHF7HYDHLHQ84Isp / eSy9vu + Xq6cT12wxg1A == ] [域名:.mcore.com] [路径:/] [到期时间:太阳9月18日10:38:59 ICT 2011]“。 非法域属性“mcore.com”。 原产地:“goklik.co.id”
我意识到这里涉及到Cookie。 但我不明白警告意味着什么。 我也不知道如何解决它(Cookie没有被拒绝)。 HOpe有一些亮光可以帮助你清除我的想法….:D
你无法“修复”它。 该网站正在尝试设置一个不允许设置的cookie,而您正在使用的apache客户端库正在告诉您它。
当域名为mcore.com
时,它正在尝试为mcore.com
设置cookie
也许为时已晚,但我遇到了同样的问题,我找到了一些帮助我解决问题的方法,只需将Cookie策略设置为浏览器兼容性:
httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
以下是可能的值:
cookie策略为给定类型或版本的cookie提供相应的cookie管理interfrace。
默认情况下使用RFC 2109规范。 可以在适当时选择其他支持的规范,或者在需要时设置默认值
提供以下规格:
BROWSER_COMPATIBILITY
:与常见的cookie管理实践兼容(即使它们不符合100%标准)NETSCAPE
:符合Netscape cookie草案RFC_2109
:符合RFC2109(默认)IGNORE_COOKIES
:不要自动处理cookie
在httpclient
4.3之前, Jonathan Silva
的回答很酷。
但是由于 httpclient
4.3,API似乎发生了很大变化,下面的代码可以工作:
RequestConfig customizedRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build(); HttpClientBuilder customizedClientBuilder = HttpClients.custom().setDefaultRequestConfig(customizedRequestConfig); CloseableHttpClient client = customizedClientBuilder.build(); // customized client,
我正在使用http客户端4.5.2,这是设置cookie规范,以轻松解决我的问题。 如何实例化客户端的示例:
httpClient = HttpClients.custom() .setDefaultRequestConfig(RequestConfig.custom() // Waiting for a connection from connection manager .setConnectionRequestTimeout(10000) // Waiting for connection to establish .setConnectTimeout(5000) .setExpectContinueEnabled(false) // Waiting for data .setSocketTimeout(5000) .setCookieSpec("easy") .build()) .setMaxConnPerRoute(20) .setMaxConnTotal(100) .build();
只是想改进Eric的答案,因为它没有直接解决我的场景,但将CookieSpecs更改为IGNORE_COOKIES解决了我的问题。
RequestConfig customizedRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); HttpClientBuilder customizedClientBuilder = HttpClients.custom().setDefaultRequestConfig(customizedRequestConfig); CloseableHttpClient client = customizedClientBuilder.build(); // customized client,
因为在我的HttpClient 4.5版本中,CookieSpecs.BROWSER_COMPATIBILITY已经折旧。