HttpClient httpClient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
// ...
HttpResponse response1 = httpClient.execute(method1, httpContext);
// ...
HttpResponse response2 = httpClient.execute(method2, httpContext);
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
// ...
HttpResponse response1 = httpClient.execute(method1, httpContext);
// ...
HttpResponse response2 = httpClient.execute(method2, httpContext);
本文介绍了一种使用HttpClient进行会话管理的方法。通过创建DefaultHttpClient实例,并利用BasicCookieStore存储Cookies,结合BasicHttpContext来实现跨请求保持状态。示例代码展示了如何在两次HTTP请求间维护相同的CookieStore。
1198

被折叠的 条评论
为什么被折叠?



