473,395 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Obtaining a cookie value from a logged in user on my website

37
I am trying to obtain the values of cookies from a particular website. The problem is, when I run the following code, it only loops through 4 of 10 cookie values present. Furthermore, the values are turning up as deleted: Reference

Expand|Select|Wrap|Line Numbers
  1.  
  2. HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("http://www.mysite.com");
  3. Request.CookieContainer = new CookieContainer();
  4. HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
  5.  
  6. foreach (Cookie cook in Response.Cookies)
  7.        {                          
  8.            Console.WriteLine("{0} = {1}", cook.Name, cook.Value);           
  9.        }
  10.  
Then I came across this code, which states it will loop through every cookie value and obtain its values: Reference

Expand|Select|Wrap|Line Numbers
  1.  
  2. int loop1, loop2;         
  3.        HttpCookieCollection MyCookieColl;
  4.        HttpCookie MyCookie;
  5.        MyCookieColl = Request.Cookies;
  6.  
  7.        String[] arr1 = MyCookieColl.AllKeys; 
  8.        for (loop1 = 0; loop1 < arr1.Length; loop1++)
  9.        {
  10.            MyCookie = MyCookieColl[arr1[loop1]];          
  11.            String[] arr2 = MyCookie.Values.AllKeys;
  12.        }
  13.  
The problem with the above, I can't seem to find a reference to "Request". I am using the libraries "using System.Net and
using System.Web", so I am unsure why Request is not being recognized.

My goal is to retrieve one cookie value so that when a user from my website logs in, my C# application will recognize that user.

Thanks for any help!
Aug 28 '12 #1
7 2494
PsychoCoder
465 Expert Mod 256MB
Try System.Web.HttpContext.Current.Request
Aug 28 '12 #2
chopin
37
I tried using this line here:

Expand|Select|Wrap|Line Numbers
  1. MyCookieColl = System.Web.HttpContext.Current.Request.Cookies;
  2.  
Unfortunately, upon executing that line of code, it prematurely opened my form, and terminated the program. I'm not sure how to proceed in obtaining the cookie values.
Aug 28 '12 #3
PsychoCoder
465 Expert Mod 256MB
Quick question, is this an ASP.NET Web Application or a WinForms application?
Aug 28 '12 #4
chopin
37
This is WinForms. I am developing an executable standalone program that will connect directly to my website's MySql database.
Aug 28 '12 #5
chopin
37
I've done some more investigating, and it appears that this line of code is deleting my cookie values, which is the cause for my problem:

Expand|Select|Wrap|Line Numbers
  1. HttpWebResponse Response = HttpWebResponse)Request.GetResponse();
I'm at a loss at what to do, it seems like the code in my first codeblock example is the correct way to accomplish what I need, the only problem being the above line deletes the values of the cookies I need.
Aug 28 '12 #6
chopin
37
Maybe this will help clarify what I am trying to do. The equivalent code in PHP to obtain a cookie value is:

Expand|Select|Wrap|Line Numbers
  1. $_COOKIE['cookie_name']
The above will output a cookie value if that cookie exists. That's it! Is there an equivalent function in C#?
Aug 28 '12 #7
chopin
37
I've actually figured out how to do this by utilizing PHP instead. PHP has a built in function that can capture a cookie value by name, called $_COOKIE['cookie_name']. When a PHP page is loaded, I execute MySQL insert commands to input temp data into a table. C# will execute the PHP page executing the PHP commands which will insert the cookie value into a table when the C# program is loaded. Since C# can grab information from a MySQL database, this is a valid solution. Here is the code that will launch the script:

Expand|Select|Wrap|Line Numbers
  1. System.Diagnostics.Process.Start("www.example.com/script.php");
Then C# will read the values from the temp table, storing the cookie values into a public static variable. My program will now recognize a username based on that cookie variable during the duration the program is opened.

I don't know of any other way at the moment to capture a cookie value, at least natively in a C# WinForms application. But the above solution is 100% accurate and works well.
Aug 29 '12 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: N?ant Humain | last post by:
I have just begun learning Python so that I can write a simple script to make modification of a file used by another Python script easier. This file is basically a list of regular expressions. What...
2
by: terry | last post by:
Hi, When I store cookie value "12:05:33" and read it again, I find the value become "12%3A05%3A33", how to read or convert it correctly? Thanks!
6
by: zoltix | last post by:
Hi, I would like to access to execute specific commands but the privileges are not enough from an aspx Page. Because this service run as IU_IISSERVER, therefore aspx hasn’t access to these...
0
by: Kevin | last post by:
Hello, all experts: May I ask you some questions? First, i'm programming a software of auto-authentication by using VB.NET tool. This software's functions is similar to do the account login...
5
by: mvr | last post by:
Hi all How to get the Windows current logged user name using Classic ASP. If no direct way are there any work arounds. Thanks mvr
3
by: korque | last post by:
Hi I'm gathering information from web page, first I get headers and store recieved cookies then I get source with file()-function. Is there easy way to send cookie value back to host on each...
1
by: rajunarender01 | last post by:
hi i am working with the usercontrol and want to update the cookie value in the user control how to do it
0
by: javaBookWorm | last post by:
203.114.10.66 - - "GET...
3
by: webster5u | last post by:
Miss/Mr. Programmer, I have a idea to remove the cookie value from a website after all browser closed. It is like session kept in server and eliminate after all browser closed. My purpose is...
2
by: ramuksasi | last post by:
Hi, I am unable to read a cookie value from the cookies. i have set cookie value from javascript in scripting side and try to read cookie value from coding part ie. default.aspx.vb. i have shown...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.