6.1 Manajer Cookie
Nalika sampeyan wis ngerti, server http bisa ngirim cookie bebarengan karo respon, lan sampeyan kudu bisa karo wong-wong mau. Utawa kosok balene, server http nunggu klien ngirim Cookie lan sampeyan kudu nambahake menyang panyuwunan http. Mesthi, sampeyan bisa nindakake iki langsung liwat header (penangan), nanging HttpClient nawakake mekanisme sing luwih trep - CookieHandler
. Sampeyan bisa njaluk nggunakake cookieHandler()
. Tuladha:
HttpClient client = HttpClient.newBuilder( URI.create("https://codegym.cc")).build();
CookieHandler handler = client.cookieHandler();
CookieHandler minangka kelas abstrak, mula umume nggarap implementasi CookieManager. Sing, mung duwe sawetara cara sing bisa sampeyan entuk obyek CookieStore. Sampeyan bisa nggarap ing mangsa ngarep:
HttpClient client = HttpClient.newBuilder( URI.create("https://codegym.cc")).build();
CookieHandler handler = client.cookieHandler();
CookieManager manager = (CookieManager) handler;
CookieStore store = manager.getCookieStore();
CookieStore minangka antarmuka sing nduweni cara ing ngisor iki:
add()
get()
getCookies()
remove()
removeAll()
Aku ora bakal njlèntrèhaké kanthi rinci, kita wis nutupi HttpClient kanthi rinci. Yen dumadakan sampeyan pancene mbutuhake, banjur dokumentasi ing kelas CookieManager bisa ditemokake ing tautan: