作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要存储 cookie 才能在关闭应用程序后保持 session 。为此,我需要能够保存 cookie,然后重新创建它。我目前的问题是,我无法正确创建 BasicClientCookie。要么域为空,要么其他数据为空。
测试代码:
List<Cookie> cookies = this.httpClient.getCookieStore().getCookies();
if (cookies != null) {
Log.print("SAVING TO COOKIE CACHE");
for (Cookie cookie : cookies) {
String cookieString = cookie.getName() + "=" + cookie.getValue()
+ "; domain=" + cookie.getDomain();
Log.d("Saving cookie: %s", cookieString);
String[] keyValueSets = cookieString.split(";");
if (keyValueSets.length == 0 || keyValueSets[0] == null) {
break;
}
String[] keyValue = keyValueSets[0].split("=");
if (keyValue.length == 0 || keyValue[0] == null) {
break;
}
String key = keyValue[0];
Log.d("Adding cookie with key %s and value %s", key, cookieString);
Cookie co = new BasicClientCookie(key, cookieString);
Log.print("Cookie domain: " + co.getDomain());
Log.print("Cookie name: " + co.getName());
Log.print("Cookie val: " + co.getValue());
Log.print("Cookie comment: " + co.getComment());
Log.print("Cookie commentUrl: " + co.getCommentURL());
Log.print("Cookie path: " + co.getPath());
Log.print("Cookie version: " + co.getVersion());
Log.print("Cookie date: " + co.getExpiryDate());
CookieManager.getInstance().setCookie(cookie.getDomain(), cookieString);
this.cacheManager.setString(TAG, cookie.getDomain());
}
}
CookieSyncManager.getInstance().sync();
日志:
03-28 13:56:35.405: E/PRINTER(9714): SAVING TO COOKIE CACHE
03-28 13:56:35.405: D/Debug(9714): Saving cookie: ci_session=a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22123e722edd173a295c1e8f3efae7c0ba%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A13%3A%2287.32.150.212%22%3Bs%3A10%3A%22user_agent%22%3Bb%3A0%3Bs%3A13%3A%22last_activity%22%3Bi%3A1364471789%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3B%7D784e1b95d3e5b11fb969d137a67676bb; domain=test.address.lt
03-28 13:56:35.405: D/Debug(9714): Adding cookie with key ci_session and value ci_session=a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22123e722edd173a295c1e8f3efae7c0ba%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A13%3A%2287.32.150.212%22%3Bs%3A10%3A%22user_agent%22%3Bb%3A0%3Bs%3A13%3A%22last_activity%22%3Bi%3A1364471789%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3B%7D784e1b95d3e5b11fb969d137a67676bb; domain=test.address.lt
03-28 13:56:35.405: E/PRINTER(9714): Cookie domain: null
03-28 13:56:35.405: E/PRINTER(9714): Cookie name: ci_session
03-28 13:56:35.405: E/PRINTER(9714): Cookie val: ci_session=a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22123e722edd173a295c1e8f3efae7c0ba%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A13%3A%2287.32.150.212%22%3Bs%3A10%3A%22user_agent%22%3Bb%3A0%3Bs%3A13%3A%22last_activity%22%3Bi%3A1364471789%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3B%7D784e1b95d3e5b11fb969d137a67676bb; domain=test.address.lt
03-28 13:56:35.405: E/PRINTER(9714): Cookie comment: null
03-28 13:56:35.405: E/PRINTER(9714): Cookie commentUrl: null
03-28 13:56:35.405: E/PRINTER(9714): Cookie path: null
03-28 13:56:35.405: E/PRINTER(9714): Cookie version: 0
03-28 13:56:35.405: E/PRINTER(9714): Cookie date: null
03-28 13:56:35.890: D/Debug(9714): Saving cookie: PHPSESSID=97eotn8964tgvv9j80uk8dddh5; domain=test.address.lt
03-28 13:56:35.890: D/Debug(9714): Adding cookie with key PHPSESSID and value PHPSESSID=97eotn8964tgvv9j80uk8dddh5; domain=test.address.lt
03-28 13:56:35.890: E/PRINTER(9714): Cookie domain: null
03-28 13:56:35.890: E/PRINTER(9714): Cookie name: PHPSESSID
03-28 13:56:35.890: E/PRINTER(9714): Cookie val: PHPSESSID=97eotn8964tgvv9j80uk8dddh5; domain=test.address.lt
03-28 13:56:35.890: E/PRINTER(9714): Cookie comment: null
03-28 13:56:35.890: E/PRINTER(9714): Cookie commentUrl: null
03-28 13:56:35.890: E/PRINTER(9714): Cookie path: null
03-28 13:56:35.890: E/PRINTER(9714): Cookie version: 0
03-28 13:56:35.890: E/PRINTER(9714): Cookie date: null
我做错了什么?
最佳答案
我找不到关于 BasicClientCookie 的任何好的文档,所以我只是决定在关闭程序时以及稍后启动程序时将所有组件(如 getDomain()、getValue() 等)存储在由特殊符号序列分隔的字符串中再次,我从内存中加载该字符串,解析单独的值,然后仅使用方法 setDomain()、setValue() 等来创建有效的 cookie。这似乎现在工作得很好......
关于java - 创建 BasicClientCookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15681328/
我需要存储 cookie 才能在关闭应用程序后保持 session 。为此,我需要能够保存 cookie,然后重新创建它。我目前的问题是,我无法正确创建 BasicClientCookie。要么域为空
我使用以下代码 fragment 将 cookie 添加到 Android 中的 http 请求,使用 Android 的 DefaultHttpClient 发送: CookieStore cook
我正在使用 spymemcached 2.8.1 版读取 cookie 对象,但我一直遇到以下异常: app[web.1]: WARN net.spy.memcached.transcoders.Se
我是一名优秀的程序员,十分优秀!