How can I get the cookies from HttpClient?

前端 未结 5 1783
难免孤独
难免孤独 2020-12-14 06:08

I am using HttpClient 4.1.2

HttpGet httpget = new HttpGet(uri); 
HttpResponse response = httpClient.execute(httpget);

So, how can I get the

5条回答
  •  忘掉有多难
    2020-12-14 06:26

    Based on the example in the initial question, the way to access the CookieStore after executing an HTTP request, is by using the HttpContext execution state object.

    HttpContext will reference a cookie store (new if no CookieStore was specified in the HttpClientBuilder) after a request is executed.

    HttpClientContext context = new HttpClientContext();
    CloseableHttpResponse response = httpClient.execute(request, context);
    CookieStore cookieStore = context.getCookieStore();
    

    This applies on httpcomponents-client:4.3+ when the ClosableHttpClient was introduced.

提交回复
热议问题