一尘不染

如何在Java中将Cookie与HttpGet一起发送

java

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

阅读 214

收藏
2020-12-03

共1个答案

一尘不染

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

2020-12-03