Setting and retrieving cookies using HttpComponents

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(); 
+4
source share
1 answer

just found the following example demonstrating the use of cookies in the login example: HttpComponents example with Cookies

Perhaps you can change this so that the server responds to the contents of the sent cookie, so you can evaluate whether the cookie was actually sent to the server. (You send a cookie with "foo", "bar" or some randomization values, and the server will respond with "bar", "foo" or something like that)

+2
source

Source: https://habr.com/ru/post/1435134/


All Articles