如何在Java中使用HttpGet发送cookie

时间:2010-08-01 19:10:59

标签: java android cookies httpclient

我正在尝试发送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();
}

2 个答案:

答案 0 :(得分:3)

这实际上是HttpClient 4.0.1的正确实现,我刚刚没有得到正确的cookie。

答案 1 :(得分:0)

您的MyCookieStorageClass.getCookie()方法确实返回了具有正确域名和路径属性的Cookie吗?