I have seen many questions about using cookies with url fetch on App Engine, but many people doing it incorrectly, and no confirmed-as-working solutions on Java App Engine.
All I want to do is:
- Make one request
- Get the cookies from that request.
- Make a second request, with those cookies.
How hard can it be??
This is easy to do on the dev server, because the dev server copies cookies across requests automatically – and indeed it’s impossible to stop it doing this. But the real App Engine urlfetch service does not do this.
Use
to create the request object.
The
doNotFollowRedirects()is essential for some reason. I don’t understand why.Then get the set-cookie headers from the response (ones whose names
equalsIgnoreCase("Set-Cookie")) and create a correspondingCookieheader – trimming off everything after the semicolon in each Set-Cookie header, if there is one, and then concatenating all the cookies together with;as a separator.This doesn’t correctly deal with expiration etc., but this should be enough for most purposes.