What is a good way to share a session between two HTTPClients in a Java program?

半城伤御伤魂 提交于 2019-12-25 02:03:30

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/12097216/what-is-a-good-way-to-share-a-session-between-two-httpclients-in-a-java-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!