java - HttpUrlConnection: Cookies Show in CookieStore but not in the POST Header -
in code below running post request on website. dont understand why cookie shows via cookiemanager, not show in post header. see comments in code.
can kindly explain missing?
cookiemanager cm = new cookiemanager(null, cookiepolicy.accept_all); cookiehandler.setdefault(cm); ... connection = (httpurlconnection) url.openconnection(); connection.setrequestmethod("post"); outputstream outputstream = connection.getoutputstream(); outputstream.write(urlparams.getbytes(charset)); // clear cookies prove not old request. cm.getcookiestore().removeall(); if (connection.getresponsecode() != httpurlconnection.http_ok) throw new exception("invalid response code."); // no cookie prints here: log.d("aero", connection.getheaderfields().tostring()); list<httpcookie> cookies = cm.getcookiestore().getcookies(); (httpcookie cookie : cookies) { if (cookie.getname().equals("asp.net_sessionid")) { // cookie here log.d("aero", cookie.tostring()); } }
ok clear head morning have managed solve 1 myself. problem response 302 redirect , redirected page had no cookie in response header.
i needed use:
connection.setinstancefollowredirects(false);
to ensure reading response original header not redirected one.