469,628 Members | 1,022 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,628 developers. It's quick & easy.

c#:collectin cookies frm httpwebresponse when multiple set-cookies headers r returned

I am coding a http proxy in c# and using httpwebrequest class to send request to the web server, when the server returns multiple set-cookie headers then the httpwebresponse object combines them all in to a single set-cookie key-value pair.
I am tryin to get the value of the headers in the following way.

Expand|Select|Wrap|Line Numbers
  1.  
  2. for (int i = 0; i < response.Headers.Count; ++i)
  3.  Console.WriteLine("{0}:{1}", response.Headers.Keys[i], response.Headers[i]);
  4.  
  5.  
when i send a request to mypsace.com the server returns the following :

set-cookie headers
Set-Cookie: MYUSERINFO=; domain=.myspace.com; expires=Wed, 19-Jan-2005 08:28:17 GMT; path=/
Set-Cookie: MYUSERINFO=; domain=myspace.com; expires=Wed, 19-Jan-2005 08:28:17 GMT; path=/
Set-Cookie: USER=; domain=.myspace.com; expires=Wed, 19-Jan-2005 08:28:17 GMT; path=/
Set-Cookie: USER=; domain=myspace.com; expires=Wed, 19-Jan-2005 08:28:17 GMT; path=/

when i try to get the same through my code it looks like this :

Set-Cookie:MYUSERINFO=; domain=.myspace.com; expires=Wed, 19-Jan-2005 08:28:17 G
MT; path=/,MYUSERINFO=; domain=myspace.com; expires=Wed, 19-Jan-2005 08:28:17 GM
T; path=/,USER=; domain=.myspace.com; expires=Wed, 19-Jan-2005 08:28:17 GMT; pat
h=/,USER=; domain=myspace.com; expires=Wed, 19-Jan-2005 08:28:17 GMT

I tried using the response.cookies method but gave up on it as it was processing all the cookie values, i need to get the exact values of the raw headers to proxy it to the browser.
Any help would be much appreciated, this is my first project in .net and i think i hav hit a road block here.
thanks in advance
Feb 1 '08 #1
5 10571
Plater
7,872 Expert 4TB
Why not just use the .Cookies property to see what cookies are trying to be set and the combine from there?
Feb 1 '08 #2
Why not just use the .Cookies property to see what cookies are trying to be set and the combine from there?
hi plater, thanks for the reply.
i did think about it as well, it would involve more code and i might possible miss out something.i have seen websites setting really weird cookies.i was hoping if there was a more direct way through which i could directly access the raw set-cookie headers, that way possibilities of introducing bugs would become less.
Feb 1 '08 #3
Plater
7,872 Expert 4TB
Well I didn't see how the two headers you showed were different?
I would guess that the extra line breaks are just because you are using Console, which will insert them to line-wrap as needed.

EDIT: seems it doesn't make any entries in the Cookies section, weird.

Here's what I got from myspace.com
Expand|Select|Wrap|Line Numbers
  1. Content-Length: 51280
  2. Cache-Control: private
  3. Content-Type: text/html; charset=utf-8
  4. Set-Cookie: MYUSERINFO=; domain=.myspace.com; expires=Wed, 19-Jan-2005 08:28:17 GMT; path=/,MYUSERINFO=; domain=myspace.com; expires=Wed, 19-Jan-2005 08:28:17 GMT; path=/,USER=; domain=.myspace.com; expires=Wed, 19-Jan-2005 08:28:17 GMT; path=/,USER=; domain=myspace.com; expires=Wed, 19-Jan-2005 08:28:17 GMT; path=/,MSCulture=IP=74.92.34.25&IPCulture=en-US&PreferredCulture=en-US&PreferredCulturePending=&Country=US&ForcedExpiration=0&timeZone=0&USRLOC=QXJlYUNvZGU9NjAzJkNpdHk9QnJvb2tsaW5lJkNvdW50cnlDb2RlPVVTJkNvdW50cnlOYW1lPVVuaXRlZCBTdGF0ZXMmRG1hQ29kZT01MDYmTGF0aXR1ZGU9NDIuNzQ5NCZMb25naXR1ZGU9LTcxLjY2ODEmUG9zdGFsQ29kZT0wMzAzMyZSZWdpb25OYW1lPU5I; domain=.myspace.com; expires=Fri, 08-Feb-2008 16:42:19 GMT; path=/,SessionDDF1=42cd8b692c61f9128296c4dc372a5e9655c15692e06749df; domain=.myspace.com; path=/,SessionDDF2=16e5eae9c9f477affed68ed69aa6e7414885361be2a4277b; domain=.myspace.com; path=/; expires=Friday, 01-Aug-08 16:42:19 GMT
  5. Server: Microsoft-IIS/6.0
  6. X-AspNet-Version: 2.0.50727
  7. X-Server: 66dc745d1825fe9ae98e6af186c04a019bd5bfcab67cbd10
  8. Date: Fri, 01 Feb 2008 16:42:19 GMT
  9.  
Feb 1 '08 #4
Well I didn't see how the two headers you showed were different?
I would guess that the extra line breaks are just because you are using Console, which will insert them to line-wrap as needed.

EDIT: seems it doesn't make any entries in the Cookies section, weird.

Here's what I got from myspace.com
Expand|Select|Wrap|Line Numbers
  1. Content-Length: 51280
  2. Cache-Control: private
  3. Content-Type: text/html; charset=utf-8
  4. Set-Cookie: MYUSERINFO=; domain=.myspace.com; expires=Wed, 19-Jan-2005 08:28:17 GMT; path=/,MYUSERINFO=; domain=myspace.com; expires=Wed, 19-Jan-2005 08:28:17 GMT; path=/,USER=; domain=.myspace.com; expires=Wed, 19-Jan-2005 08:28:17 GMT; path=/,USER=; domain=myspace.com; expires=Wed, 19-Jan-2005 08:28:17 GMT; path=/,MSCulture=IP=74.92.34.25&IPCulture=en-US&PreferredCulture=en-US&PreferredCulturePending=&Country=US&ForcedExpiration=0&timeZone=0&USRLOC=QXJlYUNvZGU9NjAzJkNpdHk9QnJvb2tsaW5lJkNvdW50cnlDb2RlPVVTJkNvdW50cnlOYW1lPVVuaXRlZCBTdGF0ZXMmRG1hQ29kZT01MDYmTGF0aXR1ZGU9NDIuNzQ5NCZMb25naXR1ZGU9LTcxLjY2ODEmUG9zdGFsQ29kZT0wMzAzMyZSZWdpb25OYW1lPU5I; domain=.myspace.com; expires=Fri, 08-Feb-2008 16:42:19 GMT; path=/,SessionDDF1=42cd8b692c61f9128296c4dc372a5e9655c15692e06749df; domain=.myspace.com; path=/,SessionDDF2=16e5eae9c9f477affed68ed69aa6e7414885361be2a4277b; domain=.myspace.com; path=/; expires=Friday, 01-Aug-08 16:42:19 GMT
  5. Server: Microsoft-IIS/6.0
  6. X-AspNet-Version: 2.0.50727
  7. X-Server: 66dc745d1825fe9ae98e6af186c04a019bd5bfcab67cbd10
  8. Date: Fri, 01 Feb 2008 16:42:19 GMT
  9.  
well actually the mypsace.com sever sets 5-6 separate set-cookie headers and when u get it thru response.headers then all of them are pushed in to a single set-cookie header, i want to split them back and send them to the browser as 5 seperate set-cookie headers like the server had sent, but since every site has its own style for cookies am not sure i can write a regex which will work all the time. dats y i was looking for an alternate method.
Thanks again.
Feb 2 '08 #5
Plater
7,872 Expert 4TB
Hmm, I would guess it was done because the collection object requires a unique key for the headers. (Really I suspect it is bad form to have multiple header lines with the same name: 'Set-Cookie')
At any rate, it seems to comma delimenate them so a simple .Split() on that string for commas should give you all the various cookie headers.


EDIT: I just noticed a problem with this, guess it's not as simple as i thought.
So using:
Expand|Select|Wrap|Line Numbers
  1. string n = "";
  2. string[] pieces = resp.GetResponseHeader("Set-Cookie").Split(',');
  3. for (int i = 0; i < pieces.Length; ++i)
  4. {
  5.    n += "Set-Cookie: " + pieces[i] + "\r\n";
  6. }
  7.  
I was able to get:
Expand|Select|Wrap|Line Numbers
  1. Set-Cookie: MYUSERINFO=; domain=.myspace.com; expires=Wed
  2. Set-Cookie:  19-Jan-2005 08:28:17 GMT; path=/
  3. Set-Cookie: MYUSERINFO=; domain=myspace.com; expires=Wed
  4. Set-Cookie:  19-Jan-2005 08:28:17 GMT; path=/
  5. Set-Cookie: USER=; domain=.myspace.com; expires=Wed
  6. Set-Cookie:  19-Jan-2005 08:28:17 GMT; path=/
  7. Set-Cookie: USER=; domain=myspace.com; expires=Wed
  8. Set-Cookie:  19-Jan-2005 08:28:17 GMT; path=/
  9. Set-Cookie: MSCulture=IP=74.92.34.25&IPCulture=en-US&PreferredCulture=en-US&PreferredCulturePending=&Country=US&ForcedExpiration=0&timeZone=0&USRLOC=QXJlYUNvZGU9NjAzJkNpdHk9QnJvb2tsaW5lJkNvdW50cnlDb2RlPVVTJkNvdW50cnlOYW1lPVVuaXRlZCBTdGF0ZXMmRG1hQ29kZT01MDYmTGF0aXR1ZGU9NDIuNzQ5NCZMb25naXR1ZGU9LTcxLjY2ODEmUG9zdGFsQ29kZT0wMzAzMyZSZWdpb25OYW1lPU5I; domain=.myspace.com; expires=Mon
  10. Set-Cookie:  11-Feb-2008 14:33:09 GMT; path=/
  11. Set-Cookie: SessionDDF1=cc057dfe6d288a6385cf9b3706676a3c5168edc0b1fd4516; domain=.myspace.com; path=/
  12. Set-Cookie: SessionDDF2=d4284c7faeaa883d1c57e9f40849a63d6dc1394e38b5fbe9; domain=.myspace.com; path=/; expires=Monday
  13. Set-Cookie:  04-Aug-08 14:33:09 GMT
  14.  
Feb 4 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Randy Chow | last post: by
1 post views Thread by Jason Manfield | last post: by
3 posts views Thread by Nuno Magalhaes | last post: by
1 post views Thread by stewart.fay | last post: by
1 post views Thread by Morgan Cheng | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.