我只能检索 3 个设置 cookie 中的 1 个,你能指出可能的错误吗?代码和相应的日志是:
BasicClientCookie cookieprotected = null;
for (int i=0; i < headers.length; i++) {
Header h = headers[i];
if(h.getName().equals("Set-Cookie"))
{
Log.i(TAG, "Header names: "+h.getName());
Log.i(TAG, "Header Value: "+h.getValue());
cookieprotected = new BasicClientCookie("Set-Cookie",h.getValue());
cookieStore.addCookie(cookieprotected);
System.out.println("cookie protected " + cookieprotected.getValue().toString());
}
}
System.out.println("cookie protected 2 " + cookieprotected.getValue().toString());
List<Cookie> cookies = cookieStore.getCookies();
Log.i("CustomHttpClient","Cookies size= " + cookies.size());
for (int i = 0; i < cookies.size(); i++) {
Cookie cookie = cookies.get(i);
Log.i("CustomHttpClient","Local cookie: " + cookie);
Cookie mSessionCookie = cookie;
Log.i("CustomHttpClient",""+cookie.getValue());
}
日志输出:
09-17 14:57:57.763: I/Cookie(781): Header names: Set-Cookie
09-17 14:57:57.763: I/Cookie(781): Header Value: JSESSIONID=0000u0yVi-4M4su-sq2Tblj9buK:16uiuppr9; Path=/
09-17 14:57:57.773: I/Cookie(781): Header names: Set-Cookie
09-17 14:57:57.773: I/Cookie(781): Header Value: PD-H-SESSION-ID=4_1_zAHen1PYrVQ+ae7Woskqy0IEcSu09rpXmIVNIGptI6tvFyNn; Domain=.net; Path=/
09-17 14:57:57.773: I/Cookie(781): Header names: Set-Cookie
09-17 14:57:57.773: I/Cookie(781): Header Value: PD-ID=gFqSdwaTUqT2mpo0wpdPG/yzH6a9XWz3j96w9r2OmTfCLHuOmYH9viGLMVXvL+LNVOG6Pvj1tvQ8c37FoeBNc7l+jPsvIQPjLIA3yzk3v65Z5Tk+oUTe7T5MxvMKMxaX964ykT7n4Z1zmZ29HHiL7p70tgiotAWLa8XtmKAlnBZF2TLjMx3GSc5+ROvQVvrM/y94zCC63UYfYWK+t80CAR2fFnGI4V6XAS1I4gKWgcqF29u8YaJs2voaqeHlMeZKAK3sw14jo6WAaZQ95M5t3B4vR+L0Ew/EG4nS89Z+j3eJxDGTesmKEI00wyRL3RQvCF5IqY0JrsmFpZS9m8Ewhu2r0AdKbmefugwHXWLMobwrVMcCJRHjdrOzkH8Jcyx9ewYxSaseuPT11wJqnJmd+bcgmOrgqtveyjBLFtoTK90sIkPku/ZtSzTMf0IS3oNgBsvvLxKDz13Joj3JkqAdq2Ophd8UlWaB+ufb95+1qfywAWvYyS2iIiNueiDUex0xU7Rz9XJwD4FphMr2/ZF9h7djOH9fErmQ+Gehd0R/NW7mtOBWAv5gSPMTxq7jpqYLD0BkFRE+VNMuk8p6LYWXThcMiGM7A15U4pmObdxGkypd3deZluHG6t8fFkS8nZ7Mzyw9WrzmKiKKWumzs/mpIskirsHtK8UQUDs+9Hz9HuLAHvwraYRkBURtt6IBQALyZsYjcahHgsQg1wY3igHXuSVHVwJMmcXalZGTsCSuyHbeURx7vgfofjkw5HBm3unNPlPsq/SLe9mIymuiFddQLJiqIZzM3CDaTHdWVokO9TY+yFNkXH3VfwPsoYTQj8e36uHERoYYWqngs3Zd3t9SH2CA+RX0RAXRRH3ZcPU/NAlvo3RaZ7d/JjFfHrVnMceFzrYw7nYqg3Tsb+bjcV3VzO57s2gs9W+VRxftJVuXGqq65Wpesp50px1GGJJcn3Fd4zGc+YlcShQ=; Domain=.stage.net; Path=/; Secure
09-17 14:39:36.443: I/CustomHttpClient(715): Cookies size= 1
09-17 14:39:36.453: I/CustomHttpClient(715): Local cookie: [version: 0][name: Set-Cookie][value: PD-ID=7hvYg9rz86x7CuZts2+Z0rujGr9L0sdjfkw8Asd7O19F0x1KrkOW3Y2aqrHB+QVI8eofkCf7gPRhGzfkROQ44rc6LVmchNrThOUxe6PlE47GCMTwQ0HSLcxrWHDCkVRZuW0OsMspaiB4Jo/q06QtJzxd8wSgwyXeRj2uOYwAXFa6F6x+FnbkPhvTuOTYB7l+eh396gLLOLpcfGbliT/AUEHPUtax4pCgjpTtpVSlzGaQjpqZEWiJ1B/qexNQ9VV5+OFHGxdJ1JfphLmNbYN7p1LSS7KLYp3v9vL0gdiZz1zsMFJgceqkqcCbzyxQ7Lrk3UoAOazTaXiqotB5seuDM5hksMDfDUx9ANAh+0xuHsvklkH6MTTkFRrkMXhT5ySqxttiAZ9yYGVnLFeAREYMXT9dp3F/icNsd+AD6G/ZdA6tBaezt5l6GrE4HSJ8zrsHv5m+NoYpCm4NDCRYb1N2oxgsByZY2P0amr74WmfiVSQqZc0bZtyu5vqXhw4gdV48KfZe7WmYUsfJarwk4Fs/NMqUCNKI7MWW6HnT9LEg1g0oghgrQsj3+md91jL68C6PKlutIcqW/UKhvoR37b5b2f9yUflxAsDj/azbfBWP4OqQgbnEs9k+NLng9D9DceWSrhnmu7V1B3//ih665pupcFzKvGIcE8pSHRUKQW+QocaCdSWQSgB0FtjfbSNP316kQO1kujnBZnkPSbxpAw72HTvVA4Lhnq6o6Wm7KymO3HTn/NYiWAIMcG2VsT6rULPuMWQ99t1IP4pUnuvdxfXpdGOujEjt9CRhhsZhmhdC33TRoUu8X2Kw4SVHLuC58VOo20SXRKtWy7IuKbG7ojcysxt355f6GhFdS8wHzUA7OYsSADB9YA46pY8bWop5t2Aj8F/FD37eJ6u5RSjZdKSAVlu0s3cLo6ffr5HhKT9ngYo2puUf7R1GLF5BbViYt2dUzHrGmNXIN3g=; Domain=.stage..net; Path=/; Secure][domain: null][path: null][expiry: null]
09-17 14:39:36.453: I/CustomHttpClient(715): PD-ID=7hvYg9rz86x7CuZts2+Z0rujGr9L0sdjfkw8Asd7O19F0x1KrkOW3Y2aqrHB+QVI8eofkCf7gPRhGzfkROQ44rc6LVmchNrThOUxe6PlE47GCMTwQ0HSLcxrWHDCkVRZuW0OsMspaiB4Jo/q06QtJzxd8wSgwyXeRj2uOYwAXFa6F6x+FnbkPhvTuOTYB7l+eh396gLLOLpcfGbliT/AUEHPUtax4pCgjpTtpVSlzGaQjpqZEWiJ1B/qexNQ9VV5+OFHGxdJ1JfphLmNbYN7p1LSS7KLYp3v9vL0gdiZz1zsMFJgceqkqcCbzyxQ7Lrk3UoAOazTaXiqotB5seuDM5hksMDfDUx9ANAh+0xuHsvklkH6MTTkFRrkMXhT5ySqxttiAZ9yYGVnLFeAREYMXT9dp3F/icNsd+AD6G/ZdA6tBaezt5l6GrE4HSJ8zrsHv5m+NoYpCm4NDCRYb1N2oxgsByZY2P0amr74WmfiVSQqZc0bZtyu5vqXhw4gdV48KfZe7WmYUsfJarwk4Fs/NMqUCNKI7MWW6HnT9LEg1g0oghgrQsj3+md91jL68C6PKlutIcqW/UKhvoR37b5b2f9yUflxAsDj/azbfBWP4OqQgbnEs9k+NLng9D9DceWSrhnmu7V1B3//ih665pupcFzKvGIcE8pSHRUKQW+QocaCdSWQSgB0FtjfbSNP316kQO1kujnBZnkPSbxpAw72HTvVA4Lhnq6o6Wm7KymO3HTn/NYiWAIMcG2VsT6rULPuMWQ99t1IP4pUnuvdxfXpdGOujEjt9CRhhsZhmhdC33TRoUu8X2Kw4SVHLuC58VOo20SXRKtWy7IuKbG7ojcysxt355f6GhFdS8wHzUA7OYsSADB9YA46pY8bWop5t2Aj8F/FD37eJ6u5RSjZdKSAVlu0s3cLo6ffr5HhKT9ngYo2puUf7R1GLF5BbViYt2dUzHrGmNXIN3g=; Domain=.stage.net; Path=/; Secure
这里
String [] cookie = h.getValue().split("=");
cookieprotected = new BasicClientCookie(cookie[0], cookie[1]);
这是因为您始终对 Cookie 使用相同的名称("Set-Cookie"),从而导致连续的 Cookie 覆盖以前的 Cookie
尝试以下代码,它将存储自动检索到的cookie。
cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
if(cookieStore != null){
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
}
response = client.execute(httpPost,httpContext);