如何在 Java 中与 HttpGet 一起发送 cookie
我试图将 cookie 与 HttpGet 请求一起发送,但每次尝试都无法成功发送。我也尝试直接修改标题,这是我的代码:
DefaultHttpClient httpClient = new DefaultHttpClient();
CookieStore store = new BasicCookieStore();
store.addCookie(MyCookieStorageClass.getCookie());
httpClient.setCookieStore(store);
HttpGet httpGet = new HttpGet("http://localhost/);
try {
// Execute HTTP Get Request
HttpResponse response = httpclient.execute(httpGet);
String responseData = ResponseHandler.getResponseBody(response);
} catch (IOException e) {
e.printStackTrace();
}
I am trying to send a cookie along with my HttpGet request, but everytime I try I havent been able to successfully send it. I also tried to modify the headers directly, here is my code:
DefaultHttpClient httpClient = new DefaultHttpClient();
CookieStore store = new BasicCookieStore();
store.addCookie(MyCookieStorageClass.getCookie());
httpClient.setCookieStore(store);
HttpGet httpGet = new HttpGet("http://localhost/);
try {
// Execute HTTP Get Request
HttpResponse response = httpclient.execute(httpGet);
String responseData = ResponseHandler.getResponseBody(response);
} catch (IOException e) {
e.printStackTrace();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这实际上是 HttpClient 4.0.1 的正确实现,我只是没有获得正确的 cookie。
This is actually the correct implementation for the HttpClient 4.0.1, I had just had not been getting the correct cookie.
您的
MyCookieStorageClass.getCookie()
方法是否返回具有正确域和路径属性的 Cookie?Your
MyCookieStorageClass.getCookie()
method do return a Cookie with correct domain and path attribute?