Is there any way to get raw response http header?
The getHeaderField()
method doesn't work for me, because server spits multiple 'Set-Cookie' and some of them get lost.
Is there any way to get raw response http header?
The getHeaderField()
method doesn't work for me, because server spits multiple 'Set-Cookie' and some of them get lost.
The easy way is to use the
getHeaderFields()
method ofURLConnection
. Here is some code that does something equivalent.You're asking this in the context of
java.net.URLConnection
, is it? No, obtaining the raw HTTP response headers is not possible withURLconnection
. You'll need to fall back to low-level Socket programming. Here's an SSCCE, just copy'n'paste'n'run it.To avoid SO being overloaded by everyone trying this snippet, here's how the output will look like:
To learn more about sending HTTP requests the low-level way, read the HTTP specification.
However, you probably want to make use of
getHeaderFields()
method instead to retrieve a header with multiple values. ThegetHeaderField()
namely only returns the last value, as per the linked API doc.Not exactly 'raw' but concise:
IF you worry that some of the headers are getting lost use:
PS: Better late than never. :)
Late to the party, but here's the simplest solution. Just implement CookieStore. (or use the default implementation and let it take care of adding the cookies to your subsequent calls.)
http://docs.oracle.com/javase/7/docs/api/java/net/CookieStore.html
Set your cookie store as the default cookie manager
And every new cookie will appear to you in add() in your CookieStore. I had the same problem with params being overwritten by having the same name "Set-Cookie" in a single request, and now I get both the cookie and the sessionId.