Link to home
Start Free TrialLog in
Avatar of shieldguy
shieldguyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to read cookie subkey from CookieContainer

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

Avatar of Rejojohny
Rejojohny
Flag of United States of America image

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

ASKER

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
Avatar of Ted Bouskill
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.
Sorry Cookie not 'Cooked'
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 ?
ASKER CERTIFIED SOLUTION
Avatar of Rejojohny
Rejojohny
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Mate