问题
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