android cookies

本文探讨了Android环境下使用HttpClient进行网络请求时的Cookie管理策略,包括Cookie存取策略、CookieSpecFactory、CookieSpecRegistry、CookieStore等组件的使用方法,并详细介绍了如何实现本地Cookie的保存与复用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 CookieSyncManager


CookieSpecFactory




CookieSpecRegistry




CookieStore






android登录Web以及登录保持,cookie管理相关
http://www.2cto.com/kf/201209/154696.html




Android 上的 HttpClient 的 Cookie 存取策略  CookieSpecFactory
http://www.oschina.net/code/snippet_12_9850




Android: HttpClient与Webview共享cookies
http://blog.youkuaiyun.com/totogogo/article/details/7309565




Android----http请求工具类
http://blog.youkuaiyun.com/riveram/article/details/7690223




HttpClient 教程 (三)
http://www.cnblogs.com/loveyakamoz/archive/2011/07/21/2113246.html




HttpClient4学习笔记
http://www.16kan.com/post/226958.html




HttpClient4的cookie rejected问题,以及如何消除该warning输出
http://www.cnblogs.com/lexus/archive/2012/02/29/2373905.html






HttpClient httpclient = new DefaultHttpClient();
// 创建cookie store的本地实例
CookieStore cookieStore = new BasicCookieStore();
// 创建本地的HTTP内容
HttpContext localContext = new BasicHttpContext();
// 绑定定制的cookie store到本地内容中
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpget = new HttpGet("http://www.google.com/");
// 作为参数传递本地内容
try {
HttpResponse response = httpclient.execute(httpget, localContext);
if(response.getStatusLine().getStatusCode()==200)
{

String result = EntityUtils.toString(response.getEntity());

Log.i("sssssssss", result);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}














private static HashMap<String,String>  CookieContiner=new HashMap<String,String>() ;
    /**
* 保存Cookie
* @param resp
*/
    public void SaveCookies(HttpResponse httpResponse)
    {
    Header[] headers = httpResponse.getHeaders("Set-Cookie");
    String headerstr=headers.toString();
        if (headers == null)
            return;


        for(int i=0;i<headers.length;i++)
        {
        String cookie=headers[i].getValue();
        String[]cookievalues=cookie.split(";");
        for(int j=0;j<cookievalues.length;j++)
        {
        String[] keyPair=cookievalues[j].split("=");
        String key=keyPair[0].trim();
        String value=keyPair.length>1?keyPair[1].trim():"";
        CookieContiner.put(key, value);
        }
        }
    }
    /**
     * 增加Cookie
     * @param request
     */
    public void AddCookies(HttpPost request)
    {
        StringBuilder sb = new StringBuilder();
        Iterator iter = CookieContiner.entrySet().iterator();
        while (iter.hasNext()) {
          Map.Entry entry = (Map.Entry) iter.next();
          String key = entry.getKey().toString();
          String val = entry.getValue().toString();
          sb.append(key);
          sb.append("=");
          sb.append(val);
          sb.append(";");
        }
        request.addHeader("cookie", sb.toString());
    } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值