在 Java 程序中的两个 HTTPClient 之间共享会话的好方法是什么? [英] What is a good way to share a session between two HTTPClients in a Java program?

查看:36
本文介绍了在 Java 程序中的两个 HTTPClient 之间共享会话的好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一个 Android/Java 应用程序,它可以同时打开两个 Apache HTTP 连接.我一直在开发一个聊天应用程序,一个连接几乎总是在运行(服务器长时间轮询;它只会在做出更改后响应),以及使用相同会话将数据发送到同一站点的第二个请求(由于需要登录").

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.

在 StackOverflow 上进行了大量搜索后,我还没有找到一个很好的方法来做到这一点.如何实现?

After plenty of searching on StackOverflow, I have not found a good way to do this. How can this be accomplished?

推荐答案

以下示例从头开始创建 HTTPClient.然后它从另一个客户端中取出 cookie,并将其添加到自己的客户端中.我发现这在 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 程序中的两个 HTTPClient 之间共享会话的好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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