473,379 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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 10832
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

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

Similar topics

4
by: hanaa | last post by:
Hi I'm tyring to create a collection of an object inside an object I have tried to use the code written in the following URL...
0
by: Randy Chow | last post by:
Hello everyone I was wondering if anyone has come across my problem? I programmed a Windows Service that uses httpWebRequest and httpWebResponse in VB.NET. Everything works fine until I use the...
13
by: Jason Manfield | last post by:
For some URLs (e.g.http://v3.espacenet.com/origdoc?DB=EPODOC&IDX=WO2005028634&F=0&QPN=WO2005028634), the content length for the HttpWebResponse I get with request.GetResponse in empty. The...
1
by: Jason Manfield | last post by:
Why does HttpWebResponse.CharacterSet always return ISO-8859-1? I am accessing a Chinese Web site (http://cn.yahoo.com) -- View Source in IE shows the character set to be "gb2312", but the...
3
by: Nuno Magalhaes | last post by:
Hello, In a simple thread I have a code like the one below: public void ProtectionRun() { while(true) { //Sleep thread for one minute //Thread.Sleep(60000); HttpWebRequest
2
by: STEVE.KING | last post by:
Greetings: I need to download an executable and some other binary files (a C++ DLL) from a virtual directory in my WinForms application. No-Touch Deployment manages my .NET code fine but...
1
by: Joe | last post by:
I'm creating an application that HTTP posts some form data, and is supposed to read the response. When I actually do this form posting from a web page, I get the response exactly as I would...
1
by: stewart.fay | last post by:
Dear all, I am using the HttpWebRequest and HttpWebRequest to send an XML request to an ASPX page. When I use the browser and add my XML request the request is posted fine and an answer is...
1
by: Morgan Cheng | last post by:
I found one issue about HttpWebResponse. I tried to get a huge webpage (i.e. http://wx.msn.com) with HttpWebRequest timeout set; reading the response stream is still controlled by timeout....
13
by: deepunarayan | last post by:
Hi I have Problem in ASP. I have created a Multi choice Question page in ASP with Submit button. When I submit my page the User Selected values will be taken to the other page where validation...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.