Good day.
There is a site to which I send get
and post
requests using HttpClient
,
public String doGet(String url) { HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet request = new HttpGet(url); HttpProtocolParams.setUserAgent(client.getParams(), "My funcy UA"); try { HttpResponse response = client.execute(request, localContext); System.out.println("Response Code:" + response.getStatusLine().getStatusCode()); Header[] cookiesArray = response.getHeaders("Set-Cookie"); for (int i = 0; i < cookiesArray.length; i++) { Cookie cookie = new BasicClientCookie(cookiesArray[i].getName().toString(), cookiesArray[i].getValue() .toString()); cookieStore.addCookie(cookie); } System.out.println(); List<Cookie> cookieList = cookieStore.getCookies(); for (int i = 0; i < cookieList.size(); i++) { System.out.println("Cookie " + "name : " + cookieList.get(i).getName() + " value :" + cookieList.get(i).getValue()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
In the body of each get
request I receive cookies
and put them in the CookieStore
. The question is how to properly manage them? Do I have to look at the cookies with which the next request should be sent and delete the extra ones from the CookieStore
? Or do I collect all the incoming cookies from each request and move on to the next one?
Thank.