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.

    1 answer 1

    Should I myself look with which cookies the next request should be sent

    I pass cookies simply:

     HttpGet httpGet = new HttpGet(mUrl); httpGet.addHeader("Cookie", "chbx=guest; jurl="+jurl+"; ucss=normal; bbuserid="+bbuserid+"; bbpassword="+bbpassword+"; bbusername="+bbusername); HttpResponse response = client.execute(httpGet); 

    bbuserid, bbusername , etc. - I take from the cookies collected earlier

    • OK, what do we know about transmitting cookies? How do you save them after the application is closed? I tried to deal with CookieManager and CookieSyncManager, but I could not figure out how to work with this mechanism correctly. - Opalosolo