WebRequest Question

graham23s

Member
Joined
Feb 22, 2009
Messages
13
Programming Experience
1-3
Hi Guys,

I have managed to learn about the httpwebrequest way of logging into a website as opposed to the webbrowser control.

code:

PHP:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ' Start [create the request]
        Dim varRequest As HttpWebRequest = CType(WebRequest.Create(Uri.EscapeUriString(url.Text)), HttpWebRequest)

        ' Set the method we'll be using
        varRequest.Method = "POST"

        ' Create the string we'll be posting
        Dim varPostData As String = "txt-email=" & Trim(user.Text) & "&txt-pass=" & Trim(pass.Text) & "&submit-login=Sign+in+to+Your+Account"

        MessageBox.Show(varPostData)
        ' Store data in a byte array using ASCII standard
        Dim varBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(varPostData)

        ' Set the content type of the data being posted
        varRequest.ContentType = "application/x-www-form-urlencoded"

        ' Set the content length of the string being posted
        varRequest.ContentLength = varBytes.Length

        ' Setting KeepAlive = false results in sending a Connection: Close header to the server
        varRequest.KeepAlive = False

        ' Send the request! When using the POST method, you must get the request stream, write the data to be posted, and close the stream 
        Dim varStream = varRequest.GetRequestStream()

        varStream.Write(varBytes, 0, varBytes.Length)
        varStream.Close()

        ' Get the HTTP response, casting to a type of HttpWebResponse
        Dim response As HttpWebResponse = CType(varRequest.GetResponse(), HttpWebResponse)
        'MessageBox.Show(response.StatusCode)
        ' Load the HTTP response into a StreamReader object
        Dim sr As New System.IO.StreamReader(response.GetResponseStream)

        ' Read the stream returned from the response object
        Dim HTMLResponse As String = Trim(sr.ReadToEnd())

        ' Print the contents to a multiline text box
        debugger.Text = HTMLResponse

        'Close the response object and StreamWriter (IMPORTANT)
        response.Close()
        sr.Close()

    End Sub
When i echo out the: HTMLResponse code it shows the the code of the sites login page rather than the page you get to once you initially log in, does that mean i haven't logged in via the code above? or is there a step i have missed out?

thanks for any help guys

Graham
 
Top Bottom