System.Net vs. System.Web namespaces and Cookie/HttpCookie?


I have a C# assembly I am writing for the client side.  I can use the System.Net namespace to request a webpage, and can get the CookieCollection from the HttpWebResponse object.  However the cookies I get are System.Net.Cookie not System.Web.HttpCookie.  And this is a problem because the Cookie class only exposes a Value property, not Values like the HttpCookie does.  So when I need to read multiple cookie key/value pairs in my application I cannnot.

Is there any way to work around this?

In my assembly only the first 3 classes of System.Web are available.  Do I have a bad installation, or is because I am writing a Windows Form application not a web form application?

Thanks, Gerry

Who is Participating?
gregoryyoungConnect With a Mentor Commented:
its because 1 is the client side representation of a cookie and the other is the server side representation ...

multiple values in a cookie are still set within a single cookie they just use delimiters
gerry99Author Commented:
Okay,  Then I guess my question is how do I access multiple cookie values from the client side?  If I use the Cookie.Value property I only get back the first value as a string.  I want the 2nd value.
cookies get a form like a URL ... NAME=VALUE&NAME=VALUE

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

gerry99Author Commented:

First off I appreciate the time you've taken to try to answer this.  Now I'm going to pull out some code.

Essentially my problem is that the Value property of the Cookie class only returns the 1st value, not the 2nd or subsequent values.

My Code:

string baseURL = "";
Uri swUri = new Uri(baseURL);

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(swUri);
// set cookie container (required)
CookieContainer cc = new CookieContainer(10);
req.CookieContainer = cc;

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
CookieCollection cookies = resp.Cookies;

foreach( Cookie cookie in cookies )
                System.Console.println( "domain " + cookie.Domain );
      System.Console.println( "comment " + cookie.Comment );
      System.Console.println( "value " + cookie.Value );
      System.Console.println( "expired " + cookie.Expired );

I get the cookie my website set, by requesting a page from it.  (This is running as a .NET assembly, that will be by other programmers to build client applications that connect to our server programs.)

My cookie looks like:


The Cookie Value property only returns Value1, and I want Value2.  I can't change the website to only write one value.  Any ideas?
have a url I can test with ?
gerry99Author Commented:
No I don't have a URL I can give you for testing at this time.  I think the answer hinges on the fact that my code is client side code, and System.Web is server side code.  Do you know of a link that properly discusses the restrictions that MS has planned for when writing client side code?

So it stands that HttpCookie.Values would be way more usefull than Cookie.Value, but I can't use it.  It may be that which value I get depends on which specific Value of the cookie, that page I am accessing is setting.  I'm not sure.  Working with the webmaster for this site, I should be able to work around the problem.

gerry99Author Commented:
I've moved on from trying to solve this problem.  Basically what I wanted to do, would not do the right thing in the context I wanted to use it.  We added an http query/response to return from our server the information I wanted without using cookies.  

It is true that the server side and client side cookie APIs differ slightly in C#.  With the server side handling multiple values properly.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.