简体   繁体   English

Android HttpClient和Cookies

[英]Android HttpClient and Cookies

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. 我在Android中遇到HttpClient问题:通过使用以下代码,我想通过webview登录来使用之前已设置的cookie。 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. 但是当我在httppost或httpget中使用cookie时,它不使用登录数据。 but these cookies actually should be enough to receive that page for which a login is necessary, shouldn't they? 但这些cookie实际上应该足以接收需要登录的页面,不应该这样吗? 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. 我不确定是否需要以特殊方式将cookie发送到服务器,或者是否足以将其加载到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);          

And if I run the code I only receive the login page of that site, so it didn't accept the cookie. 如果我运行代码我只收到该网站的登录页面,所以它不接受cookie。

Thanks for help in advance 提前感谢您的帮助

Greets, timo 招呼,蒂莫

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. 使它适用于我的是为每个复制的cookie添加域。 (BasicClientCookie cookie.setDomain(String)) (BasicClientCookie cookie.setDomain(String))

My util function: 我的util函数:

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);
 ...

if you still have this problem, be careful with the given cookies, some might be malformed, check these two sites out: 如果你仍然有这个问题,小心给定的cookie,有些可能会格式不正确,请检查这两个网站:

http://www.codeproject.com/Articles/3106/On-The-Care-and-Handling-of-Cookies http://www.codeproject.com/Articles/3106/On-The-Care-and-Handling-of-Cookies

this one helped me: Getting "Set-Cookie" header 这个帮助了我: 获得“Set-Cookie”标题

It seems you are copying the cookies correctly, and generally you don't need to do anything special for HttpClient to send the cookies. 您似乎正在正确地复制cookie,通常您不需要为HttpClient做任何特殊的事情来发送cookie。 However, some of those may be bound to a session, and when you open a new connection with HttpClient you open a new session. 但是,其中一些可能绑定到会话,当您使用HttpClient打开新连接时,您将打开一个新会话。 The server will probably ignore cookies that don't match the current session. 服务器可能会忽略与当前会话不匹配的cookie。 This might work if the session ID is in a cookie and you are able to get into the same session, but you really need to know exactly what the server does. 如果会话ID是在一个cookie,你能够进入同一个会话这可能会实现,但你真的需要确切地知道服务器的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM