Android 4.4 KitKat 未收到 cookie

2024-02-25

在我的应用程序中,我向服务器发送 POST 请求,并从服务器接收响应。 我从响应中收集了不同的 cookie,特别是用户信息。因此,我发送登录请求并在服务器响应后接收 cookie,以保持登录。在 Android 4.3 及更低版本中,我可以正常接收 cookie,并且用户登录成功。但在Android 4.4中,用户登录成功,但没有收到cookie。

Android 是否做了一些重大改变才导致这种情况发生?如果有人有任何建议,下面是我的代码。

private URL urlObj;
private HttpURLConnection connection;
private DataOutputStream dataOs;


private ArrayList<String> schools;
private ArrayList<Post> schoolPosts;
private String schoolID;
private String name;

private String userLoginCookie, sessionSeedCookie, sessionUidCookie, sPrefCookie;

private Context context;
private CookieStore store;

public DataParser(Context _context) {
    context = _context;
}

//First call whenever connecting across the user's network
private void establishConnection() throws IOException {
    urlObj = new URL(url);
    connection = (HttpURLConnection) urlObj.openConnection();
    CookieManager cookieManager = new CookieManager();

    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cookieManager);
    store = cookieManager.getCookieStore();

    getCookies();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
    connection.setRequestProperty("Cookie", sessionSeedCookie+";"+sessionUidCookie+";"+userLoginCookie+";"+sPrefCookie);
    connection.setDoOutput(true);
    connection.setUseCaches(false);

    dataOs = new DataOutputStream(connection.getOutputStream());
}

//Called after communication is complete
private void disconnectAll() throws IOException {
    connection.disconnect();
    dataOs.close();
}

private void getCookies() {
    SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
    userLoginCookie = settings.getString(USER_LOGIN, BLANK);
    Log.d(LOG, "Cookie: "+userLoginCookie);
    sessionSeedCookie = settings.getString(SESS_SEED, BLANK);
    Log.d(LOG, "Cookie: "+sessionSeedCookie);
    sessionUidCookie = settings.getString(SESS_UID, BLANK);
    Log.d(LOG, "Cookie: "+sessionUidCookie);
    sPrefCookie = settings.getString(S_PREF, "sPref="+BLANK);
    Log.d(LOG, "Cookie: "+sPrefCookie);
}

private void updateCookies() {
    SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();

    List<HttpCookie> cookieList = store.getCookies();
    for(int i=0; i<cookieList.size(); i++) {
        if(cookieList.get(i).getName().equals(USER_LOGIN)) 
            editor.putString(USER_LOGIN, cookieList.get(i).toString());
        else if(cookieList.get(i).getName().equals(SESS_SEED)) 
            editor.putString(SESS_SEED, cookieList.get(i).toString()); 
        else if(cookieList.get(i).getName().equals(SESS_UID)) 
            editor.putString(SESS_UID, cookieList.get(i).toString());
        else
            Log.d(LOG, "Found Extra Cookie: "+cookieList.get(i).getName());
    }
    sPrefCookie = settings.getString(S_PREF, "sPref="+BLANK);

    editor.commit(); //Save changes to the SharedPreferences
}       

//Logins User into Walkntrade
public String login(String email, String password) throws IOException {
    establishConnection(); //Instantiate all streams and opens the connection

    String query= "intent=login&password="+password+"&email="+email+"&rememberMe=true";
    dataOs.writeBytes(query);
    Log.d(LOG, "" + connection.getResponseCode());
    updateCookies();

    String response = readInputAsString(connection.getInputStream());

    Log.d(LOG, "Connection Status: "+response);

    disconnectAll();
    return response;
}

//Logs user out of Walkntrade
public void logout() throws IOException { 
    establishConnection();

    String query = "intent=logout";
    dataOs.writeBytes(query);
    Log.d(LOG, "" + connection.getResponseCode());
    updateCookies();

    disconnectAll();
}

//Returns user login status
public static boolean isUserLoggedIn(Context _context) {
    SharedPreferences settings = _context.getSharedPreferences(PREFS_NAME, 0);
    boolean isUserLoggedIn = settings.getBoolean(DataParser.CURRENTLY_LOGGED_IN, false);

    return isUserLoggedIn;
}

public String getUserName() throws IOException{
    establishConnection();

    String query = "intent=getUserName";
    dataOs.writeBytes(query);
    Log.d(LOG, ""+connection.getResponseCode());
    updateCookies();

    String response = readInputAsString(connection.getInputStream());

    disconnectAll();
    return response;
}

public String getUserAvatar() throws IOException {
    establishConnection();

    String query = "intent=getAvatar";
    dataOs.writeBytes(query);
    Log.d(LOG, ""+connection.getResponseCode());
    updateCookies();

    String response = readInputAsString(connection.getInputStream());

    disconnectAll();
    return response;
}

在 KitKat 4.4 更新之前,这段代码对我来说工作得很好 -

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

//handle cookies
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);

它在 4.4.2 后崩溃了(至少那时我注意到了),并且不再收到 cookie。在打开 urlConnection 之前简单地移动 CookieManager 和 CookieHandler 再次修复它..奇怪的是它以前工作过!例如。

//handle cookies
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 4.4 KitKat 未收到 cookie 的相关文章

随机推荐