最好的存储方式&使用带有HTTPPost&的cookie机器人了HTTPClient?

时间:2014-02-17 02:46:52

标签: android http cookies

我正在编写一个登录系统,可以让用户永久登录(直到用户名或密码不正确),但我遇到了cookie存储问题。我想要做的是将cookie存储在本地存储中(可能是共享首选项)。虽然我不知道从哪里开始。这是我的主要HTTP Post函数。

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    try {
        // Add your data
        httppost.setHeader("X-Requested-With", "XMLHttpRequest");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        StringBuffer sb = new StringBuffer();
        BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        rd.close();
        return sb.toString();
    } catch (Exception e) {
        //TODO: WIP
        e.printStackTrace();
    }

我想首先设置cookie(当然,如果有的话),然后我想在httppost执行后重新保存它们。我在哪里可以做到这一点?

编辑大约有4个已保存的Cookie。

1 个答案:

答案 0 :(得分:1)