什么是Java程序共享两个HTTPClients之间的会话的好方法? [英] What is a good way to share a session between two HTTPClients in a Java program?
问题描述
我一直在开发开辟了两个同时Apache的HTTP连接一个Android / Java应用程序。我一直在开发一个聊天应用程序,以及一个连接几乎始终运行(服务器是长轮询,一旦进行了更改,那就只能响应),和第二个请求将数据发送到同一个网站,使用同一个Session(因)是需要的是登录。
I have been developing an Android/Java application that opens up two simultaneous Apache HTTP connections. I have been developing a Chat application, and one connection is almost always running (the server was long polling; it would only respond once changes were made), and a second request to send data to the same site, using the same Session (due to being "logged in") was needed.
很多在计算器上搜索后,我还没有找到一个很好的办法做到这一点。这怎么可能实现呢?
After plenty of searching on StackOverflow, I have not found a good way to do this. How can this be accomplished?
推荐答案
下面的例子从零开始创建了HTTPClient。然后,它从另一个客户端拉出来饼干,并把它添加到自己的。我发现这个工程没有在Android中顺利。
The following example creates an HTTPClient from scratch. It then pulls the cookies out from another client, and adds it to its own. I've found this works without a hitch in Android.
DefaultHttpClient sendClient = new DefaultHttpClient();
CookieStore originalCookies = Globals.getClient().getCookieStore();
sendClient.setCookieStore(originalCookies);
这篇关于什么是Java程序共享两个HTTPClients之间的会话的好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!