Mark

Mark

  • NA
  • 29
  • 0

Getting a cookie from a web project into a WinForm

May 2 2018 7:42 AM
Hi there,
 
I have an application which i have created.  The application is a Windows based application mainly written in C# but has some different projects within it.   I have a Form project and a web project
 
Within the web project i have created an ASP.net page which saves data typed into an input box into a cookie, i can then retrieve this data by loading it back out of the cookie as shown below:
  1. HttpCookie cookie = new HttpCookie("Name");  
  2. cookie.Value = TextBox1.Text.ToString();  
  3. cookie.Expires = DateTime.Now.AddMinutes(10);  
  4. Response.Cookies.Add(cookie);  
  5.   
  6. TextBox1.Text = Request.Cookies["Name"].Value;  
 
So my question is, in a winform created in C# how can i get hold of the cookie information?   I have seen some examples of ways to do this but they are messy and are very browser dependent, like the one shown below:
  1. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);  
  2. request.CookieContainer = new CookieContainer();  
  3.   
  4. HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
  5. response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);  
  6.   
  7. foreach (Cookie cook in response.Cookies)  
  8. {  
  9.     Console.WriteLine("Cookie:{0} = {1}", cook.Name, cook.Value);  
  10. }  
Some have also said:
  1. Properties.Settings.Default.MySettingString = "New Value";  
  2. Properties.Settings.Default.Save();  
  3. ...  
  4. string myValue = Properties.Settings.Default.MySettingString;  
That would be nice as a solution, except i have two different projects and so it won't work as properies.settings are project specific.
 
I just need a way of passing a variable from a web project into a win forms project (within the same Visual Studio solution) for a person's name in a basic computer game, hence me trying to use a cookie, all ideas are welcomed as i am probably going a bad way of doing it.  The only other way i could think of was saving to an SQL database 
 
Many thanks,
 
Mark 

Answers (6)