1

In the code below I am running a post request on a website. What I dont understand is why the cookie shows up via the cookiemanager, but it does not show up in the POST header. See my comments in the code.

Can someone kindly explain what I am 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 to prove they are not from an 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();

for (HttpCookie cookie : cookies) {
         if (cookie.getName().equals("ASP.NET_SessionId")) {
             // But we do get a cookie here
             Log.d("Aero", cookie.toString());             
          }
     }   
Reafidy
  • 8,240
  • 5
  • 51
  • 83
  • Don't you need to add the cookies to the http header? Edit: Have a look here: http://stackoverflow.com/questions/16150089/how-to-handle-cookies-in-httpurlconnection-using-cookiemanager – Robbe Roels Apr 23 '15 at 08:48
  • @RobbeRoels, yes that's what I'm trying to do. But I cant retrieve the cookie like in the article you posted. They are retrieving the cookie via connection.getHeaderFields(); which is exactly what I am doing, however the code returns a null even though the cookiestore shows there is actually a cookie there. – Reafidy Apr 23 '15 at 10:46
  • Do you want to get the cookie out of your response or put a cookie in your POST? Because connection.getHeaderFields(); gets the headerfields of your response. Use cm.getCookieStore().getCookies() (The second part, "Get Cookies form cookieManager and load them to connection") to add them to your POST to the server out of your manager. – Robbe Roels Apr 23 '15 at 11:17
  • @RobbeRoels I want to get the cookie out of the response. The response should contain a cookie which the website server creates. But I believe the cookie should be in connection.getHeaderFields() or more specifically connection.getHeaderFields().get("Set-Cookie"). But it returns null. I am trying to avoid using the cookiemanager other that for debugging to prove the cookie actually exists. – Reafidy Apr 23 '15 at 11:21
  • Sorry, I don't think I can be of any further help, I'm lacking the experience with HttpUrlConnection. I mostly used Apache's HttpClient which does most of the work for you. If you still have the chance to change your implementation I'd recommend that! – Robbe Roels Apr 23 '15 at 11:39
  • 2
    @RobbeRoels, No problem, thanks anyway. Actually I was using HttpClient but unfortunately in android it is now deprecated, hence the change to HttpURLConnection. – Reafidy Apr 23 '15 at 11:51

1 Answers1

1

Ok with a clear head this morning I have managed to solve this one myself. The problem was that the response was a 302 redirect and the redirected page had no cookie in the response header.

I needed to use:

connection.setInstanceFollowRedirects(false);

To ensure I was reading the response from the original header not the redirected one.

Reafidy
  • 8,240
  • 5
  • 51
  • 83