473,669 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

5 New Member
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:MYUSERIN FO=; 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.cookie s 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 10854
Plater
7,872 Recognized Expert Expert
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
piedpiper
5 New Member
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 Recognized Expert Expert
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
piedpiper
5 New Member
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.header s 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 Recognized Expert Expert
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
1383
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 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vaconcreatingyourowncollectionclass.as its working fine when I test it from VB.net but my application is a web-application , I tried to use the same classes that I have created and tested in VB.Net the collection counter is always set to 1 even...
0
1403
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 webresponse object. What the Windows Service does is when the timer in the windows service elapses, the request to the website is made and a response is retrieve in a simple myhttpWebrequest.GetResponse(). I don't even do anything with the...
13
9807
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 response.GetResponseStream() also empty. However, I am able to open the URL in the browser (the URL address remains the same; it is not redirected) Here is the code snippet: HttpWebRequest req = (HttpWebRequest)WebRequest.Create(pageAddress);
1
3301
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 HttpWebResponse shows it to be ISO-8859-1. Websites like www.cnn.com also show the character set to ISO-8859-1 Here is the code snippet: string pageAddress = "http://cn.yahoo.com"; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(pageAddress);
3
4657
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
14205
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 auxilliary files such as these must be downloaded "manually" in my code. My text-based files download fine but I've ran into a problem downloading an .EXE. When it downloads the file size is the same but it has been altered somehow because a) the...
1
5867
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 expect. However, when I try to do this form posting programmatically, I get a WebException where the Status is set to ServerProtocolViolation. As far as I can tell, this status tells me that the response is not a standard HTTP response. Ok, I know...
1
3443
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 retrieved. However, when I use the code below I get an timeout error on the line: oWebResponse = DirectCast(oWebRequest.GetResponse, HttpWebResponse)
1
4384
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. However, the HttpWebResponse.Close takes ages. I suppose HttpWebResponse want to do a clean resouce clean-up. So, if the website doen't finish sending chuckes of HTTP packets, the client would not return from Close(). Is there anybody clear about the...
13
2142
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 will happen. My problem is, the number of question will be dynamic and I need to validate the scores and give the final scores. I tried to use a form collection via for each and validate with my answers but could nt successful
0
8894
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8587
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2029
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1787
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.