http请求代码出现Cookie rejected错误解决方法
HttpClient 一直报WARN:
Cookie rejected: "[version: 0][name: U_TRS2][value: 00000
0be.116527ba.51510ea5.8f309118][domain: .test.com][path: /][expiry: null]". Illegal domain attribute "test.com". D
omain of origin: "test.com"
我用的是httpclient-4.1.jar,问了google多遍都无结果,最后在CSDN上的一篇博客里看到HttpClient Tutorial中的cookie管理这一章,有段说自定义CookiePolicy
public static String httpClientConnection(String url){
String returnString;
try{
CookieSpecFactory csf = new CookieSpecFactory() {
public CookieSpec newInstance(HttpParams params) {
return new BrowserCompatSpec() {
public void validate(Cookie cookie, CookieOrigin origin)
throws MalformedCookieException {
}
};
}
};
HttpPost httpPost = new HttpPost(url);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setSoTimeout(httpPost.getParams(), 60000);
HttpConnectionParams.setConnectionTimeout(httpPost.getParams(), 60000);
httpClient.getCookieSpecs().register("easy", csf);
httpClient.getParams().setParameter(
ClientPNames.COOKIE_POLICY, "easy");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entity=httpResponse.getEntity();
returnString= EntityUtils.toString(entity);
} catch (Exception e){
e.printStackTrace();
returnString="调用接口异常";
}
return returnString;
}
把上面红色标记的那段代码加到我的代码中,Cookie rejected错误问题解决了。。。。