Jump to content
  • Advertisement
Sign in to follow this  
PrettySneaky

[.net] Retrieve a cookie from the harddrive, C#

This topic is 4647 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently writing an application which is supposed to get some data from a webpage. In order to do that I want to use a cookie which is saved on the harddrive. Is it possible to load a cookie which is stored from the harddrive, or do I have to create them myself? The c#-pseuode code below should explain what i want..
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("some url");

Cookie cookie = new Cookie();

cookie = a cookie save in c:\documents and settings\username\cookies\something.txt

req.CookieContainer = new CookieContainer();
req.CookieContainer.add(cookie);

System.Net.WebResponse r = req.GetResponse();
Stream s = r.GetResponseStream();
StreamReader sr = new StreamReader(s);
string st = sr.ReadToEnd();




Share this post


Link to post
Share on other sites
Advertisement

You have to make a persistent cookie, so you have to set an expiration date. If you do not do that, it will automatically become a temporary cookie.

Create like this:


HttpCookie objCookie = new HttpCookie("MyCookie");
DateTime now = DateTime.Now;

//The following code adds a pair of keys and values:
objCookie.Values.Add("Time", now.ToString());
objCookie.Values.Add("ForeColor", "White");
objCookie.Values.Add("BackColor", "Blue");

//The following code sets the expiration time of the cookie to one hour:
objCookie.Expires = now.AddHours(1)

Response.Cookies.Add(objCookie);




And retrieve the cookie like this:



HttpCookie objCookie = Request.Cookies["myCookie"];
lblTime.Text = objCookie.Values["Time"];
lblTime.BackColor = System.Drawing.Color.FromName
(objCookie.Values["BackColor"]);



Hope this helps,

Edo

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!