如何在 Java 中与 HttpGet 一起发送 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3383367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send a cookie along with HttpGet in Java
提问by ninjasense
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:
我正在尝试将 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();
}
采纳答案by ninjasense
This is actually the correct implementation for the HttpClient 4.0.1, I had just had not been getting the correct cookie.
这实际上是 HttpClient 4.0.1 的正确实现,我只是没有得到正确的 cookie。
回答by Domenico Briganti
Your MyCookieStorageClass.getCookie()method do return a Cookie with correct domain and path attribute?
您的MyCookieStorageClass.getCookie()方法确实返回了具有正确域和路径属性的 Cookie?

