2 markdowneide markdowneide 于 2013.03.27 14:05 提问

在 android 中存储 Http cookie

我使用授权给网站开发一个Android客户端。示例代码如下:

public void run() {
    handler.sendMessage(Message.obtain(handler, HttpConnection.DID_START));
    httpClient = new DefaultHttpClient();
    HttpConnectionParams.setSoTimeout(httpClient.getParams(), 25000);
    HttpResponse response = null;
    try{            
        switch (method){
        case POST:
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeaders(headers);
            if (data != null) httpPost.setEntity(new StringEntity(data));
            response = httpClient.execute(httpPost);
            break;
        }
        processEntity(response);

    }catch(Exception e){
        handler.sendMessage(Message.obtain(handler, HttpConnection.DID_ERROR, e));

    }
    ConnectionManager.getInstanse().didComplete(this);      
}

如何保存 cookies?

2个回答

nana4ever
nana4ever   2013.04.02 11:21
已采纳

你可以从HttpResponse response中获取 cookies

String mCookies[] = response.getHeaders("cookie");

将它们添加到你的下一个请求:

HttpClient httpClient = new DefaultHttpClient();

//parse name/value from mCookies[0]. If you have more than one cookie, a for cycle is needed.
CookieStore cookieStore = new BasicCookieStore();
Cookie cookie = new BasicClientCookie("name", "value");
cookieStore.addCookie(cookie);

HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

HttpGet httpGet = new HttpGet("http://www.domain.com/"); 

HttpResponse response = httpClient.execute(httpGet, localContext);
zhenzhenzhao12
zhenzhenzhao12   2013.03.27 17:59

我是这么做的:
1.服务器设置cookie;
2.客户端从服务器获取cookie并保存;
3.客户端页面跳转是判断url是否包含服务器地址,如果包含则读取cookie

具体是通过SharedPreferences来实现保存和读取的

保存:

SharedPreferences sp = context.getSharedPreferences(Const.eBag,
                        Context.MODE_PRIVATE);

                sp.edit()
                        .putString(account + "_ticket",
                                data.getString("ticket"))
                        .putString(account + "_ID", data.getString("id"))
                        .putString(account + "_AccountID", account)
                        .putString(account + "_DisplayName",
                                data.getString("display")).commit();

读取:

String ticket = context.getSharedPreferences(Const.eBag, Context.MODE_PRIVATE).getString(Const.username + "@" + Const.domain + "_ticket", null);
            String cookieStr = context.getResources().getString(R.string.cookie_name) + "=" + "_wsut_=" + ticket + ";" + "Domain=" + context.getResources().getString(R.string.DOMAIN) + ";" + "PATH=" + context.getResources().getString(R.string.PATH) + ";" + "EXPIRES=" + context.getResources().getString(R.string.EXPIRES);
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Cookie", cookieStr);
            webView.loadUrl(tag, headers);
suannai0314
suannai0314 帮你编辑了下代码的格式,更多markdown的使用见http://ask.csdn.net/pages/markdown
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!