Store cookie in sharedpreferences

别等时光非礼了梦想. 提交于 2019-12-23 09:38:21

问题


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.

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...

This is what I've tried:

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();
            }
        }
    }
}

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));
}

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?


回答1:


I used this part:

        cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies();
        Log.v("Cookie:", cookies.toString());
        if (cookies.isEmpty()) {
        } else {
               for (int i = 0; i < cookies.size(); i++) {
                      if(cookies.get(i).getName().contentEquals("PHPSESSID")) {
                         PHPSESSID = cookies.get(i).getValue();
                      }
                   }
        }

Just use the contentEquals to get the domain name and let it match with urs and store it. Oh and i used PHPSESSID as a string which i dumped in my sharedprefs for later




回答2:


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();
           }
       }

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.



来源:https://stackoverflow.com/questions/12707655/store-cookie-in-sharedpreferences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!