Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-09-10
8
Medium Priority
?
2,239 Views
Last Modified: 2012-05-05
Arggh!

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

0
Comment
Question by:gerry99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 12031207
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
0
 
LVL 1

Author Comment

by:gerry99
ID: 12031484
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.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 12034445
cookies get a form like a URL ... NAME=VALUE&NAME=VALUE

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:gerry99
ID: 12034860
Gregory,

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 = "http://www.myhomepage.com/";
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 );
}
resp.Close();

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:

KEY1
Value119350949%2c18223%2c556915%2cgrempel
mywebsite.com/
1536
1308650496
31865174
4111566704
29661001
*
KEY2
Value2%7c0%2cY%7c0.74%7cC%7cY%7cN%7cDTS%7cUC%7c%7c%7c1
mywebsite.com/
1536
3742840192
29734523
1698328992
29661098
*

The Cookie Value property only returns Value1, and I want Value2.  I can't change the website to only write one value.  Any ideas?
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 12035027
have a url I can test with ?
0
 
LVL 1

Author Comment

by:gerry99
ID: 12060137
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.

Thanks,
Gerry
0
 
LVL 1

Author Comment

by:gerry99
ID: 12250096
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question