6.1 쿠키매니저

이미 알고 있듯이 http 서버는 응답과 함께 쿠키를 보낼 수 있으며 이를 사용하여 작업해야 합니다. 또는 그 반대의 경우, http 서버는 클라이언트가 쿠키를 보내기를 기다리고 있으므로 http 요청에 쿠키를 추가해야 합니다. 물론 헤더(핸들러)를 통해 직접 이 작업을 수행할 수 있지만 HttpClient는 보다 편리한 메커니즘인 CookieHandler. 를 사용하여 얻을 수 있습니다 cookieHandler(). 예:

HttpClient client = HttpClient.newBuilder( URI.create("https://codegym.cc")).build();
CookieHandler handler = client.cookieHandler();

CookieHandler는 추상 클래스이므로 CookieManager 구현으로 작업하는 것이 일반적입니다. 결국 CookieStore 개체를 가져올 수 있는 몇 가지 메서드만 있습니다. 나중에 작업할 수 있습니다.

HttpClient client = HttpClient.newBuilder( URI.create("https://codegym.cc")).build();
CookieHandler handler = client.cookieHandler();
CookieManager manager = (CookieManager) handler;
CookieStore store = manager.getCookieStore();

CookieStore는 다음 메서드가 있는 인터페이스입니다.

  • add()
  • get()
  • getCookies()
  • remove()
  • removeAll()

자세한 내용은 다루지 않겠습니다. 이미 HttpClient에 대해 자세히 다루었습니다. 갑자기 필요한 경우 다음 링크에서 CookieManager 클래스에 대한 문서를 찾을 수 있습니다.

클래스 쿠키 관리자

Java의 CookieManager 클래스

맞춤 CookieManager