How To Use .NET Cookies

If you want to access, read, write or otherwise store and send cookies in .NET you will need to access the request objects cookiecontainer. Once you have that in your sights, you can grab the cookiecollection and pick up the cookie that you need.

Here is an example;

Dim req As HttpWebRequest =
CType(WebRequest.Create(http://urlgoeshere.com, HttpWebRequest)
req.Method = “POST”
req.CookieContainer =
New CookieContainer
‘get the response
Dim res As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse)
‘now get the cookies
res.Cookies = req.CookieContainer.GetCookies(req.RequestUri)
Dim ck As System.Net.Cookie = res.Cookies(“CookieName”)

Once you have the cookie, it’s simple to add it to your request

‘add the cookie to the request
req.CookieContainer = New CookieContainer
req.CookieContainer.Add(ck)

So if you ever need to grab a cookie and then use that cookie in another request for authentication purposes, thats what you do…

, , , , , , , , ,

No comments yet.

Leave a Reply