?
Solved

How to read cookie subkey from CookieContainer

Posted on 2008-02-05
7
Medium Priority
?
1,036 Views
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

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

Please find the code attached

Thanks
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
 
        response.Close()
 
        Return ""
 
    End Function

Open in new window

0
Comment
Question by:shieldguy
  • 3
  • 2
  • 2
7 Comments
 
LVL 26

Expert Comment

by:Rejojohny
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 "/"
0
 
LVL 1

Author Comment

by:shieldguy
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")

thanks
0
 
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.
0
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'
0
 
LVL 1

Author Comment

by:shieldguy
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 ?
0
 
LVL 26

Accepted Solution

by:
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 ..

Rejo
0
 
LVL 1

Author Closing Comment

by:shieldguy
ID: 31428044
Thanks Mate
0

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