Android HttpClient 和 Cookie [英] Android HttpClient and Cookies

查看:26
本文介绍了Android HttpClient 和 Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android 中的 HttpClient 有问题:通过使用以下代码,我想通过 webview 登录使用之前已经设置的 cookie.所以登录数据应该在那里并且确实在那里,我测试了它.但是当我在 httppost 或 httpget 中使用 cookie 时,它​​不使用登录数据.但这些 cookie 实际上应该足以接收需要登录的页面,不是吗?我不确定是否需要以特殊方式将 cookie 发送到服务器,或者是否足以将其加载到 httpcontext 中.代码如下:

I have a problem with the HttpClient in Android: By using the following code, I want to use the cookies which are already set before by logging in through a webview. So the login data should be there and is indeed there, I tested it. But when I use the cookies in an httppost or httpget it doesn't use the login data. but these cookies actually should be enough to receive that page for which a login is necessary, shouldn't they? I'm not really sure if I need to send the cookies in a special way to the server or so or if it is enough to load it into the httpcontext. Here is the code:

DefaultHttpClient httpclient = new DefaultHttpClient();
CookieStore lCS = new BasicCookieStore();


if (CookieManager.getInstance().getCookie(pUrl) != null) {  
    String cookieString = CookieManager.getInstance().getCookie(pUrl);

    String[] urlCookieArray = cookieString.split(";");
    for (int i = 0; i < urlCookieArray.length; i++) {           
        System.out.println(urlCookieArray[i]);          
        String[] singleCookie = urlCookieArray[i].split("=");
        Cookie urlCookie = new BasicClientCookie(singleCookie[0], singleCookie[1]);
        lCS.addCookie(urlCookie);           
    }

}

HttpContext localContext = new BasicHttpContext();
httpclient.setCookieStore(lCS);
localContext.setAttribute(ClientContext.COOKIE_STORE, lCS);

HttpPost httppost = new HttpPost(pUrl);        


    // get the url connection       
try {

    StringBuilder sb = new StringBuilder();     
    HttpResponse response = httpclient.execute(httppost, localContext);     
    InputStream is = response.getEntity().getContent();         
    InputStreamReader isr = new InputStreamReader(is);          

如果我运行代码,我只会收到该站点的登录页面,因此它不接受 cookie.

And if I run the code I only receive the login page of that site, so it didn't accept the cookie.

提前感谢您的帮助

问候,蒂莫

推荐答案

我遇到了同样的问题,我使用了与问题类似的方法,但没有成功.使它对我有用的是为每个复制的 cookie 添加域.(BasicClientCookie cookie.setDomain(String))

I had the same problem and I used similar approach as in the question with no luck. The thing that made it work for me was to add the domain for each copied cookie. (BasicClientCookie cookie.setDomain(String))

我的实用函数:

public static BasicCookieStore getCookieStore(String cookies, String domain) {
    String[] cookieValues = cookies.split(";");
    BasicCookieStore cs = new BasicCookieStore();

    BasicClientCookie cookie;
    for (int i = 0; i < cookieValues.length; i++) {
        String[] split = cookieValues[i].split("=");
        if (split.length == 2)
            cookie = new BasicClientCookie(split[0], split[1]);
        else
            cookie = new BasicClientCookie(split[0], null);

        cookie.setDomain(domain);
        cs.addCookie(cookie);
    }
    return cs;
}

 String cookies = CookieManager.getInstance().getCookie(url);
 BasicCookieStore lCS = getCookieStore(cookies, MyApp.sDomain);

 HttpContext localContext = new BasicHttpContext();
 DefaultHttpClient httpclient = new DefaultHttpClient();
 httpclient.setCookieStore(lCS);
 localContext.setAttribute(ClientContext.COOKIE_STORE, lCS);
 ...

这篇关于Android HttpClient 和 Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆