Vb.net – cant login website using httpwebrequest/responce vb.net

httpwebrequesthttpwebresponsevb.net

I am a newb to vb.net(visual studio 2008).i am trying to make an app using vb.net which can be used to login to a website and browse that website without using webbrowser(i dont want to use webbrowser of vb.net).i got a code for this from net ;I have made a temporary login webpage using php and mysql in my computer(its working properly).
but when i tried to login using vb.net its not working…
because i dont know in which part of the code is not working,i am pasting the whole code here.

below is my html code for login form

    <td style="width: 188px;"><input maxlength="120" size="30" name="login" class="css"  id="login"><br>
<br>
</td>
</tr>
<tr>

<td><b>Password</b></td>
<td><input maxlength="100" size="30" name="password" class="css" id="password" type="password"><br>
<br>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="submit" value="Login" class="button" type="submit"></td>

this is the vb.net code that i got it from net.i changed the url to my localhost website..and added username and password(both root) and also this <big>Welcome

Imports System.Net
Imports System.Text
Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cookieJar As New Net.CookieContainer()
        Dim request As Net.HttpWebRequest
        Dim response As Net.HttpWebResponse
        Dim strURL As String

        Try
            'Get Cookies
            strURL = "http://localhost/login.php"
            request = Net.HttpWebRequest.Create(strURL)
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"
            request.Method = "GET"
            request.CookieContainer = cookieJar
            response = request.GetResponse()

            For Each tempCookie As Net.Cookie In response.Cookies
                cookieJar.Add(tempCookie)
            Next

            'Send the post data now
            request = Net.HttpWebRequest.Create(strURL)
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"
            request.Method = "POST"
            request.AllowAutoRedirect = True
            request.CookieContainer = cookieJar

            Dim writer As StreamWriter = New StreamWriter(request.GetRequestStream())
            writer.Write("login=root & password=root")
            writer.Close()
            response = request.GetResponse()

            'Get the data from the page
            Dim stream As StreamReader = New StreamReader(response.GetResponseStream())
            Dim data As String = stream.ReadToEnd()
            RichTextBox1.Text = data
            WebBrowser1.DocumentText = RichTextBox1.Text
            response.Close()

            If data.Contains("<big>Welcome") = True Then
                'LOGGED IN SUCCESSFULLY
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

Thanks for your help

Best Answer

This method just works for websites which use URL parameter. Make sure you can login to your site like this:

http://localhost/login.php?user=your_username&password=your_password

Also remove spaces here:

writer.Write("login=root&password=root")
Related Topic