Accept any cookies in httpclient4
I try to develop Java client to my site but can't store cookies in httpclient4, server send in head开发者_如何学Goers in get request :
Set-Cookie: PHPSESSID=ea384f86b9b89a749f1684d9d3980820; path=/
But in code after request I make :
CookieManager m = (CookieManager) CookieHandler.getDefault();
System.out.println("Count : " + m.getCookieStore().getCookies().size());
And I always obtain Count : 0
Httpclient creation :
CookieManager cookiem = new CookieManager();
cookiem.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookiem);
...
httpClient = new DefaultHttpClient(params);
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, org.apache.http.client.params.CookiePolicy.BEST_MATCH);
But I obtain same result, what is wrong?
CookieManager
is a Java 6 specific class used by JRE's internal HTTP client.
Apache HttpClient manages HTTP state differently and cannot (and probably should not) make use of Java 6 specific classes.
For details on HTTP state management with Apache HttpClient please see:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html
精彩评论