java Android HttpClient 和 Cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13847102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:09:56  来源:igfitidea点击:

Android HttpClient and Cookies

javaandroidcookieshttpclient

提问by timothy3001

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:

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

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

问候,蒂莫

回答by Daniel Backman

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

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

My util function:

我的实用功能:

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

回答by timothy3001

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”标题

回答by Nikolay Elenkov

It seems you are copying the cookies correctly, and generally you don't need to do anything special for HttpClient to send the cookies. However, some of those may be bound to a session, and when you open a new connection with HttpClient you open a new session. The server will probably ignore cookies that don't match the current session. This mightwork 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.

看来您正确地复制了 cookie,并且通常您不需要为 HttpClient 做任何特殊的事情来发送 cookie。但是,其中一些可能绑定到会话,并且当您打开一个与 HttpClient 的新连接时,您将打开一个新会话。服务器可能会忽略与当前会话不匹配的 cookie。如果会话 ID 在 cookie 中并且您能够进入同一个会话,这可能会起作用,但您确实需要确切地知道服务器的作用。