I'm trying to figure out how to set and also extract cookies using HttpComponents, but I can't find reliable documentation, especially when it comes to setting cookies in the request. I seem to be working, but at the same time I canβt confirm that the cookies I set are sent correctly.
I noticed that the cookie I set in the request is also in the CookieStore after calling client.execute (), but I'm not sure that this is only because I add it to the CookieStore before calling client.execute () (maybe , it remains in the CookieStore without actually sending the request?). Is there a good way to confirm the sending of a cookie?
HttpGet get = new HttpGet("http://example.com/"); DefaultHttpClient client = new DefaultHttpClient(); // set the cookies CookieStore cookieStore = new BasicCookieStore(); BasicClientCookie cookie = new BasicClientCookie("foo", "bar"); cookie.setDomain("example.com"); cookie.setPath("/something/"); cookieStore.addCookie(cookie); client.setCookieStore(cookieStore); // get the cookies HttpResponse response = client.execute(get); List<Cookie> cookies = client.getCookieStore().getCookies();
acvcu source share