[HELP] Webbrowser cookies to webrequest cookiecontainer

Discussion in 'Visual Basic .NET' started by carlx, Apr 18, 2013.

  1. carlx

    carlx Registered Member

    Dec 28, 2008
    Likes Received:

    I was wondering if anybody had any advice or had faced this problem themselves.

    I am logging into a website via a webbrowser in order to grab the relevent cookies that i want (Cant do webrequest because it uses ajax and javascript to login)

    Once it is logged in i want to get the cookies it sets to allow me to webrequest some further

    I cant seem to set the cookiecontainer to be the cookies from the webbrowser.

    Any advice?
  2. gimme4free

    gimme4free Executive VIP Jr. VIP Premium Member

    Oct 22, 2008
    Likes Received:
    Something like this should do it:
    Dim cookies as new cookiecontainer
    Dim WebBrowserCookies = newBrowser.Document.Cookie
    Dim SplitCookies() as string = split(WebBrowserCookies, "; ")
    For each tmpcookie as string in SplitCookies
    	if tmpcookie = nothing then continue for
    	cookies.Add(New Uri("http://site.com"), New Cookie(Split(tmpcookie,"=")(0), Split(tmpcookie,"=")(1)))
    End For
    Edit: Forgot to note, if the cookie could possibly contain an = symbol simply join the rest of the array values after 0 to create the true cookie value.
    • Thanks Thanks x 2
    Last edited: Apr 18, 2013