安卓:示例使用Cookie从HttpPost对HTTPGET [英] Android: Example for using a cookie from HttpPost for HttpGet

查看:165
本文介绍了安卓:示例使用Cookie从HttpPost对HTTPGET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用这里的例子: http://www.androidsnippets.org /snippets/36/index.html 并顺利拿到了HTTP / 1.1 OK响应了webesite我发送HttpPost随着用户凭据。不过,我无法使用HTTPGET进一步浏览本网站其他页面。

I am able to use the example here: http://www.androidsnippets.org/snippets/36/index.html and successfully get the "HTTP/1.1 OK" response for a webesite I am sending the HttpPost along with the user credentials. However, I am unable to use an HttpGet to further browse other pages on this site.

任何人都可以请让我知道,什么错。我很抱歉 - 我是很新的Java语言。

Can anyone please let me know, what's going wrong. I am sorry - I am very new to Java.

推荐答案

我的猜测是,当网站得到的帖子,并记录用户的,它设置了响应cookie,表明用户登录,并然后,需要在后续获取的这些Cookie。

My guess would be that when the website gets the Post and logs the user in, it sets cookies on the response to indicate that the user is logged in, and then requires those cookies on subsequent Get's.

您需要做类似如下(这是从一个更大的应用程序,借了这么可能无法编译正确的开箱即用)

You will need to do something like the following (this is borrowed from a bigger app so may not compile right out of the box)

DefaultHttpClient mHttpClient = new DefaultHttpClient();
BasicHttpContext mHttpContext = new BasicHttpContext();
CookieStore mCookieStore      = new BasicCookieStore();        
mHttpContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);

该设置HTTP上下文中的cookie存储,然后使用上获取的和后的这方面。例如...

This sets up a cookie store within the HTTP context, and you then use that context on Get's and Post's. For example...

HttpResponse response = mHttpClient.execute(mRequest, mHttpContext);

在幕后HTTP客户端会存储cookie的响应,并将它们添加到请求。

Under the covers the HTTP client will store cookies from responses, and add them to requests.

这篇关于安卓:示例使用Cookie从HttpPost对HTTPGET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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