如何在 Java 中与 HttpGet 一起发送 cookie

发布于 2024-09-12 08:12:16 字数 560 浏览 4 评论 0原文

我试图将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

年少掌心 2024-09-19 08:12:17

这实际上是 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.

迷雾森÷林ヴ 2024-09-19 08:12:17

您的 MyCookieStorageClass.getCookie() 方法是否返回具有正确域和路径属性的 Cookie?

Your MyCookieStorageClass.getCookie() method do return a Cookie with correct domain and path attribute?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文