[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using windows forms, is it possible to read cookies?

Posted on 2012-09-10
9
Medium Priority
?
496 Views
Last Modified: 2013-10-18
Im trying to make a program which can read data from a webpage, Im hoping I can get my web application to store a cookie which my windows application can read.

Is it possible to get a windows application to read a cookie from a webpage?

Thank you in advance :-)
0
Comment
Question by:tonelm54
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 6

Expert Comment

by:pkbugudai
ID: 38383760
0
 
LVL 11

Expert Comment

by:Guru Ji
ID: 38383763
You can use Application.GetCookie Method to read the cookie

Please follow the link below for detailed workaround

http://msdn.microsoft.com/en-us/library/system.windows.application.getcookie.aspx
0
 
LVL 6

Expert Comment

by:pkbugudai
ID: 38383786
dear!!!

----------------this is the code----------

Imports System.Net
Namespace Examples.System.Net.Cookies
	' This example is run at the command line.
	' Specify one argument: the name of the host to 
	' send the request to.
	' If the request is sucessful, the example displays the contents of the cookies
	' returned by the host.

	Public Class CookieExample
		Public Shared Sub Main(args As String())
			If args Is Nothing OrElse args.Length <> 1 Then
				Console.WriteLine("Specify the URL to receive the request.")
				Environment.[Exit](1)
			End If
			Dim request As HttpWebRequest = DirectCast(WebRequest.Create(args(0)), HttpWebRequest)
			request.CookieContainer = New CookieContainer()

			Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
			response.Cookies = request.CookieContainer.GetCookies(request.RequestUri)


			' Print the properties of each cookie.
			For Each cook As Cookie In response.Cookies
				Console.WriteLine("Cookie:")
				Console.WriteLine("{0} = {1}", cook.Name, cook.Value)
				Console.WriteLine("Domain: {0}", cook.Domain)
				Console.WriteLine("Path: {0}", cook.Path)
				Console.WriteLine("Port: {0}", cook.Port)
				Console.WriteLine("Secure: {0}", cook.Secure)

				Console.WriteLine("When issued: {0}", cook.TimeStamp)
				Console.WriteLine("Expires: {0} (expired? {1})", cook.Expires, cook.Expired)
				Console.WriteLine("Don't save: {0}", cook.Discard)
				Console.WriteLine("Comment: {0}", cook.Comment)
				Console.WriteLine("Uri for comments: {0}", cook.CommentUri)
				Console.WriteLine("Version: RFC {0}", If(cook.Version = 1, "2109", "2965"))

				' Show the string representation of the cookie.
				Console.WriteLine("String: {0}", cook.ToString())
			Next
		End Sub
	End Class
End Namespace

Open in new window


{(EDIT) Wrapped code in code tags - @tagit Topic Advisor}
0
Industry Leaders: 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!

 

Author Comment

by:tonelm54
ID: 38384025
Ok, so I have the code:-
Imports System.Net

Module Module1

    Sub Main()
        Dim url2Cookie As String = "http://localhost"
        Console.WriteLine("Specify the URL to receive the request.")

        Dim request As HttpWebRequest = CType(WebRequest.Create(url2Cookie), HttpWebRequest)
        request.CookieContainer = New CookieContainer()

        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        Dim cook As Cookie
        For Each cook In response.Cookies
            Console.WriteLine("Cookie:")
            Console.WriteLine("{0} = {1}", cook.Name, cook.Value)
            Console.WriteLine("Domain: {0}", cook.Domain)
            Console.WriteLine("Path: {0}", cook.Path)
            Console.WriteLine("Port: {0}", cook.Port)
            Console.WriteLine("Secure: {0}", cook.Secure)

            Console.WriteLine("When issued: {0}", cook.TimeStamp)
            Console.WriteLine("Expires: {0} (expired? {1})", cook.Expires, cook.Expired)
            Console.WriteLine("Don't save: {0}", cook.Discard)
            Console.WriteLine("Comment: {0}", cook.Comment)
            Console.WriteLine("Uri for comments: {0}", cook.CommentUri)

            Console.WriteLine("Version: RFC {0}", IIf(cook.Version = 1, "2109", "2965"))

            Console.WriteLine("String: {0}", cook.ToString())
        Next cook

        Console.ReadKey()
    End Sub
End Module

Open in new window


which displays no cookie information :-(

Looking at the temporary internet files there is a cookie stored for http://localhost, and if I use PHP to read the cookie files as:-
<?php
 print_r($_COOKIE);
?>

Open in new window


It displays:-
Array
(
    [user] => Alex Porter
)

Any ideas?

Thank you
0
 
LVL 43

Expert Comment

by:Rob
ID: 39448538
Are you doing this is .net or php?
0
 

Author Comment

by:tonelm54
ID: 39494035
Both.

The idea is the PHP stores the cookie on the PC, .net reads it
0
 
LVL 43

Accepted Solution

by:
Rob earned 2000 total points
ID: 39495289
Reading the cookie should be as easy as:

If (Request.Cookies("user") Is Not Nothing) Then
    Console.WriteLine("Cookie:" & Request.Cookies("user"))
End If

Open in new window


(EDIT) - Referenced here: http://msdn.microsoft.com/en-us/library/bd70eh18(v=vs.100).aspx
0
 

Author Comment

by:tonelm54
ID: 39539477
But how can I specify which cookie to read from the domain?
0
 
LVL 43

Expert Comment

by:Rob
ID: 39541306
See my code in the last post. It's reading a cookie called "user"
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

872 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