How to read cookie subkey from CookieContainer

Posted on 2008-02-05
Medium Priority
Last Modified: 2012-05-05
I am having problem reading the cookies subkey value,

I have a cookies which name is created like this but I am not able to read its index value


Please find the code attached

Public Function getValue(ByVal strCookieKey As String, ByVal strCookieIndex As String, ByVal strDomain As String) As String
        Dim request As HttpWebRequest = CType(WebRequest.Create("http://" & strDomain), HttpWebRequest)
        request.Proxy = WebProxy.GetDefaultProxy() ' If you want to use default settings of IE
        request.Timeout = -1
        request.KeepAlive = False
        request.CookieContainer = New CookieContainer()
        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        'Print the properties of each cookie.
        Dim cook As Cookie
        For Each cook In response.Cookies
            If cook.Name = "WiggleBasket" Then
                'Return cook.Value
                Return Server.HtmlEncode(cook.Values(0))
            End If
        Next cook
        Return ""
    End Function

Open in new window

Question by:shieldguy
  • 3
  • 2
  • 2
LVL 26

Expert Comment

ID: 20827764
who stored the cookie? did the same application /domain store the cookie? what was the "path" attribute of the cookie .. try setting it as "/"

Author Comment

ID: 20827810
cookie is created on the same domain but in my code i want to access the cookie value easily but iinstead i have to put a for loop and then match each cooke witn the desired cookie subkey name and then get the name

why cant i read its value like this

value = HttpContext.Current.Response.Cookies("Basket")("BasketID")

LVL 51

Expert Comment

by:Ted Bouskill
ID: 20829727
You have it backwards.  If you want to read the cookie use value = HttpContext.Current.Request.Cookies("Basket")("BasketID")

Response is used to transmit the cookie back to the user, Request is the cooked you received from the users computer.
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

LVL 51

Expert Comment

by:Ted Bouskill
ID: 20829729
Sorry Cookie not 'Cooked'

Author Comment

ID: 20830457
Actually I am reading the cookies from the other web site using the httpwebrequest then i storing them in the cookiecontainer so may be thats why the request.cookie is not available ?
LVL 26

Accepted Solution

Rejojohny earned 2000 total points
ID: 20831917
>>Actually I am reading the cookies from the other web site using the httpwebrequest
yes, that is the reason why i asked whether it is stored by the same web site .. imagine if you can read cookies stored by another website, anyone can just loop through or guess the name of the cookies stored in a client PC and use it for malicious intent .. so there is a restrictiion implemented by all browsers wherein you cannot read cookies stored by another website ..


Author Closing Comment

ID: 31428044
Thanks Mate

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

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

SingleRun is a tool that ensures that only one instance of an application is started, running it again brings the application to focus.
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…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …

599 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