dcsimg
Results 1 to 3 of 3

Thread: [RESOLVED] HTTPWebrequest problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    417

    Resolved [RESOLVED] HTTPWebrequest problem

    Hi Guys,

    What i'm doing here is to login to one of my sites and automate a few tasks, i have hit a snag on the initial login lol

    Code:

    Code:
            '// UPDATE LOG...
            updateLog("Attempting Login...")
            Dim varCookieJar As New CookieContainer()
            Dim GetRequest As HttpWebRequest = CType(WebRequest.Create("http://site.com/login"), HttpWebRequest)
            Dim GetResponse As HttpWebResponse = CType(GetRequest.GetResponse(), HttpWebResponse)
            With GetRequest
                .CookieContainer = varCookieJar
                .UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0"
                .Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
                .Headers.Add("Accept-Language", "pl,en-us;q=0.7,en;q=0.3")
                .Headers.Add("Accept-Encoding", "gzip, deflate")
            End With
            Dim GetStreamreader As New StreamReader(GetResponse.GetResponseStream)
            Dim GetHTML As String = GetStreamreader.ReadToEnd()
            formHTML.txtBoxDebugHTML.Text = GetHTML
            formHTML.Show()
            GetResponse.Close()
            GetStreamreader.Close()
    
            '// VALIDATION...
            Dim csrfToken As String = getLoginToken(GetHTML)
            If (csrfToken = "") Then
                '// UPDATE LOG...
                updateLog("There was an error retrieving the login token...")
            Else
                '// UPDATE LOG...
                updateLog("Retrieved Login Token: " & csrfToken)
            End If
            '// LOGGING IN - [Sockets]
            Dim varPOSTRequest As HttpWebRequest = CType(WebRequest.Create("http://site.com/login"), HttpWebRequest)
            Dim varPOSTData As New StringBuilder
            varPOSTData.Append(HttpUtility.UrlEncode("member_login[email]") & "=" & HttpUtility.UrlEncode(txtUser.Text) & "&" & HttpUtility.UrlEncode("member_login[_csrf_token]") & "=" & csrfToken & "&" & HttpUtility.UrlEncode("member_login[password]") & "=" & HttpUtility.UrlEncode(txtPass.Text))
            Dim varBytes As Byte() = Encoding.ASCII.GetBytes(varPOSTData.ToString)
            With varPOSTRequest
                .UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
                .Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
                .Headers.Set("Accept-Language", "en-gb;q=0.7,en;q=0.3")
                .Headers.Set("Accept-Encoding", "gzip, deflate")
                .AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
                .CookieContainer = varCookieJar
                .ContentLength = varBytes.Length
                .ContentType = "application/x-www-form-urlencoded"
                .Method = "POST"
            End With
            Dim varStream = varPOSTRequest.GetRequestStream()
            varStream.Write(varBytes, 0, varBytes.Length)
            Dim varResponse As HttpWebResponse = CType(varPOSTRequest.GetResponse(), HttpWebResponse)
            Dim varStreamreader As New StreamReader(varResponse.GetResponseStream)
            Dim POSTHTMLsource As String = varStreamreader.ReadToEnd()
            varCookieJar.Add(varResponse.Cookies)
            formHTML.txtBoxDebugHTML.Text = POSTHTMLsource.ToString
            formHTML.Show()
            varStream.Close()
            varStreamreader.Close()
    The problem is the site is saying the "csrf token is missing" but it isn't, in my debugging i have regex'd the token out from the html, i have checked posts request using fiddler, its exactly the same, i cannot see any errors in the code!

    any help would be appreciated!

    cheers guys

    Graham

  2. #2

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    417

    Re: HTTPWebrequest problem

    Fixed! this line:

    Code:
            updateLog("Attempting Login...")
            Dim varCookieJar As New CookieContainer()
            Dim GetRequest As HttpWebRequest
    I put the request right at the top by mistake lol

    cheers I

    Graham

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width