?
Solved

How do I use windows application to read a cookie entry created by a specific web application

Posted on 2009-04-22
8
Medium Priority
?
393 Views
Last Modified: 2013-12-08
I want to know how to use windows application to read a cookie entry created by a specific web application

The code below is from a web application. It creates the cookie entry if it does not already exist.

        If Not Page.IsPostBack Then

            Dim myCookie As HttpCookie = New HttpCookie("MyCode")
            If (Request.Cookies("MyCode") IsNot Nothing AndAlso Request.Cookies("MyCode").Value > 0) Then
                Dim mCode As String = ""
                urlStr = Request.Cookies("MyCode").Value
                Response.Cookies("MyCode").Expires = Now.AddDays(-2)
            Else
                myCookie.Value = "Test01"
                myCookie.Expires = Now.AddDays(365)
                Response.Cookies.Add(myCookie)
            End If
        End If

I want a windows application that will only read the entry for a decision to be taken.

Thanks.
0
Comment
Question by:Woroworo
6 Comments
 
LVL 18

Expert Comment

by:Gary Davis
ID: 24210796
The web app creates the cookie on the client's browser. The windows app will have to run on the client's PC to get to the cookie. It can not run on the web server. Is that what you want to do?
Each browser may implement its cookie storage differently so the windows app will have to be tailored to the browser being used (IE, Firefox, etc). There are cookie apps that are able do that for various browsers. For some examples, see:
http://www.freedownloadscenter.com/Network_and_Internet/Cookie_and_Cache_Managers/
Gary Davis
0
 

Author Comment

by:Woroworo
ID: 24214606
>>The web app creates the cookie on the client's browser. The windows app will have to run on the client's PC to >>get to the cookie. It can not run on the web server. Is that what you want to do?

Yes. I want the windows app to to run on the client's PC.

Thanks.
0
 

Author Comment

by:Woroworo
ID: 24214734
I want the windows app to be an independent program running on the client's PC. This is because the output of the reading will determine the decision to be taken within that same windows app.

Thanks.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Expert Comment

by:dexion432
ID: 24554403
Make a simple windows application which reads the cookies directory and searches for the cookie.

0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 24558698
You can use a function from the wininet.dll library.  You are going to need to use the URL that is associated with the cookie to retrieve the values.




' Source:
' http://www.rendelmann.info/blog/CommentView.aspx?guid=bd99bcd5-7088-4d46-801e-c0fe622dc2e5
 
Imports System
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Net
 
Public Class InternetCookies
 
  <DllImport("wininet.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
  Private Shared Function InternetGetCookie(ByVal lpszUrlName As String, ByVal lpszCookieName As String, ByVal lpszCookieData As StringBuilder, <MarshalAs(UnmanagedType.U4)> _
    ByRef lpdwSize As Int32) As Boolean
  End Function
 
  Private Shared Function RetrieveIECookiesForUrl(ByVal url As String) As String
 
    Dim cookieHeader As New StringBuilder(New String(" "c, 256), 256)
 
    Dim datasize As Integer = cookieHeader.Length
 
    If Not (InternetGetCookie(url, Nothing, cookieHeader, datasize)) Then
      If datasize < 0 Then
        Return String.Empty
      End If
 
      cookieHeader = New StringBuilder(datasize)
 
      InternetGetCookie(url, Nothing, cookieHeader, datasize)
    End If
 
    Return cookieHeader.ToString()
  End Function
 
  Public Shared Function GetCookieContainer(ByVal url As String) As CookieContainer
 
    Dim container As New CookieContainer()
    Dim uri As New Uri(url)
 
    ' CookieContainer.SetCookies expects a string with cookie key+value pairs separated
    ' by commas, and not semi-colons.
    Dim cookieHeaders As String = RetrieveIECookiesForUrl(uri.AbsoluteUri).Replace(";", ",")
    If cookieHeaders.Length > 0 Then
      container.SetCookies(uri, cookieHeaders)
    End If
 
    Return container
 
  End Function
 
End Class

Open in new window

0
 

Author Closing Comment

by:Woroworo
ID: 31573525
Thanks so much. It really solved my problem. God bless you.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

#Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
Several part series to implement Internet Explorer 11 Enterprise Mode
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

862 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