HttpGet 会自动处理 cookie 吗?
我想在使用 HttpGet 连接到服务器时保留会话,并且我需要了解它如何处理 cookie。
服务器开发人员说他自己处理所有cookies。 我使用 HttpGet 请求访问服务器,如下所示: 输入流 isResponse = null;
HttpGet httpget = new HttpGet(strUrl);
HttpResponse response = mClient.execute(httpget);
HttpEntity entity = response.getEntity();
isResponse = entity.getContent();
responseBody = convertStreamToString(isResponse);
return responseBody;
我应该做更多的事情吗?他是否自动将 cookie 放在我的设备上,并且 HttpGet 方法知道使用它以便使用 cookie 保留 sessionID?
如何检查 cookie 是否存在于我的设备上,以便了解会话是否“有效”?
如果我使用以下代码来获取 cookie:
CookieStore cookieStore = new BasicCookieStore(); // 创建本地 HTTP 上下文 HttpContext localContext = new BasicHttpContext(); // 将自定义 cookie 存储绑定到本地上下文 localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet(strUrl); HttpResponse 响应 = mClient.execute(httpget,localContext);
HttpGet 是否仍然像以前一样处理 cookie?
- 我看到DefaultHttpClient(上面代码中的mClient)有自己的CookieStore。如何保存它的 cookie 并在下次创建它时加载它们?
I would like to preserve a session while connecting to server using HttpGet and I need to understand how it handles cookies.
The server developer says that he handles all cookies stuff by himself.
I use HttpGet request to access the server as follows:
InputStream isResponse = null;
HttpGet httpget = new HttpGet(strUrl);
HttpResponse response = mClient.execute(httpget);
HttpEntity entity = response.getEntity();
isResponse = entity.getContent();
responseBody = convertStreamToString(isResponse);
return responseBody;
Should I do something more? Does he put the cookie on my device automatically and the HttpGet method knows to use it in order to keep the sessionID using the cookie?
How can I check if the cookie exist on my device, in order to know if the session is "alive"?
In case I use the following code to get the cookie:
CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet(strUrl); HttpResponse response = mClient.execute(httpget,localContext);
does the HttpGet still handles the cookies the same as before?
- I see that DefaultHttpClient (mClient in the code above) has its own CookieStore. How can I save its cookies and load them next time I create it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以...
经过几个小时的绞尽脑汁并实现了我自己的原始 CookieStore 后,我发现了 Android 异步 Http 客户端 实现,其中包括一个很好用的持久CookieStore!
只需将 jar 添加到我的项目中并按如下方式使用它:
就是这样!再次打开应用程序时,cookie 会被保存并重复使用。
谢谢詹姆斯·史密斯,无论你在哪里。你至少让一个人开心了。
如果有人对我自己的原始实现(也有效)感兴趣,那就是:
So...
After cracking my head for hours and implementing my own primitive CookieStore, I found Android Asynchronous Http Client implementation, that includes a nice PersistentCookieStore that works great!
Simply added the jar to my project and used it as follows:
and that's it! The cookies are saved and reused when the application is open again.
Thank you James Smith, where ever you are. You made at least one man happy.
If anyone interested in my own primitive implementation (that also works) here it is:
不,cookie 不会自动处理。要自动保存cookie,请使用
BasicCookeStore
有关使用的更多信息如下: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html
另请参阅此答案:Android HttpClient 持久 cookies
No, cookies are not handled automatically. To save cookies automatically use
BasicCookeStore
More info on usage is here: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html
Also see this answer: Android HttpClient persistent cookies
我不必关心cookies。当我创建
DefaultHttpClient
并执行httpclient.execute(httpget);
作为 此处对象在其上存储会话。因此,首先我进行登录,然后我可以使用 API 的私有函数。如果我想注销,我可以创建对象DefaultHttpClient
的新实例或调用注销函数。因此,如果您想使用 cookie 的信息,请使用 CookieStore,如果不只是使用单例实例来保持对象
DefaultHttpClient
I haven't had to care about cookies. When I create my
DefaultHttpClient
and do thehttpclient.execute(httpget);
as here the object stores the session on it. So first I do the login and later I can use the private functions of my API. If I want to logout, I create a new instance of my objectDefaultHttpClient
or call the logout function.So in case you want to use the cookie's information use the CookieStore, if not just use a singleton instance to keep alive the object
DefaultHttpClient