问题描述
我一直在拉我的头发试图弄清楚这一点:我作出HttpsURLConnection的使用java.net.cookiemanager来管理我的cookies(没有使用android.webkit.cookiemanager到HttpURLConnection类/ HttpsURLConnection的,因为我的方式明白?)。我需要我的长期保存的Cookie以后的连接。
I've been pulling my hair trying to figure this out: I'm making an HttpsURLConnection and using java.net.cookiemanager to manage my cookies (there's no way of using android.webkit.cookiemanager to HttpUrlConnection/HttpsUrlConnection as I have understood?). I need to save my longtime cookie to later connections.
不幸的是,我不能使用和它的PersistentCookieStore因为我需要让不可信证书(使用http://abhinavasblog.blogspot.se/2011/07/allow-untrusted-certificate-for-https.html).我单独使用他们的PersistentCookieStore尝试,但他们所使用的Apache饼干和我使用java.net饼干...
Sadly I can't use http://loopj.com/android-async-http/ and it's PersistentCookieStore because I need to allow an untrusted certificate (using http://abhinavasblog.blogspot.se/2011/07/allow-untrusted-certificate-for-https.html). I've tried using their PersistentCookieStore alone but they are using apache cookies and I'm using java.net cookies...
这是我已经试过:
cManager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
private void setSharedPreferences(){
List<HttpCookie> cookies = cManager.getCookieStore().getCookies();
if (cookies.isEmpty()) {
Log.d(tag,"no cookies received");
} else {
for (int i = 0; i < cookies.size(); i++) {
if(cookies.get(i).getName().equals("rememberMe")){
editor.putString("rememberMe", cookies.get(i).toString());
editor.commit();
}
}
}
}
而当我检索在下次应用程序启动cookie的:
And when I'm retrieving the cookie on next app launch:
SharedPreferences sharedPreferences = context.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
String rememberString = sharedPreferences.getString("rememberMe", "none");
if (!rememberString.equals("none")) {
Log.d("rememberME är inte", "none!");
URI uriToCookie = null;
try {
uriToCookie = new URI("https://myservername.com");
} catch (URISyntaxException e) {
e.printStackTrace();
}
List<HttpCookie> cookieList = HttpCookie.parse(rememberString);
cManager.getCookieStore().add(uriToCookie, cookieList.get(0));
}
该Cookie被添加到cManager但不被该服务器识别。我相信这是某种形式的解析问题。任何人都得到了解决?
The cookie is added to cManager but is not recognized by the server.. I believe there is some sort of parse problem. Anyone got the solution?
推荐答案
看着你的code后,我code是错误的,但你根本不存储整个的cookie:
After looking at your code, I code be wrong but you are simply not storing the entire cookie:
for (int i = 0; i < cookies.size(); i++) {
if(cookies.get(i).getName().equals("rememberMe")){
editor.putString("rememberMe", cookies.get(i).toString());
editor.commit();
}
}
您获得cookie的列表
长度 cookies.size()
和循环,让所有的cookies,但你提交保存键 - 值了rememberMe,而不是附加或单独的密钥存储。所以基本上,你只是你覆盖存储什么一遍又一遍。
You get the List
length of cookies with cookies.size()
and loop and get all the cookies but you commit the save key-value "rememberMe" instead of appending or storing in separate keys. So basically, you are simply overwriting what you stored over and over.
这篇关于共享preferences存储的cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!