开发者社区> 问答> 正文

android 中怎么存储 cookie

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

展开
收起
蛮大人123 2016-02-15 15:46:22 2523 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    你可以从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);
    2019-07-17 18:43:21
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载