使用httpwebrequest / responce vb.net登录网站

时间:2012-01-28 14:45:51

标签: vb.net httpwebrequest httpwebresponse

我是vb.net的新手(visual studio 2008)。我正在尝试使用vb.net创建一个应用程序,可用于登录网站并浏览该网站而不使用webbrowser(我不想使用vb.net的webbrowser)。我从网上得到了一个代码;我在我的计算机上使用php和mysql创建了一个临时登录网页(它正常工作)。 但是当我尝试使用vb.net登录时它无法工作...... 因为我不知道代码的哪一部分不起作用,我在这里粘贴整个代码。

下面是我登录表单的HTML代码

    <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>

这是我从net.i获得它的vb.net代码将url更改为我的localhost网站..并添加了用户名和密码(都是root)以及此<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

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

此方法仅适用于使用URL参数的网站。确保您可以像这样登录您的网站:

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

此处还删除空格:

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

答案 1 :(得分:1)

确保您发送的内容正确HttpWebRequest

您可以使用Live HTTP Headers plugin for FirefoxFiddler来捕获网络请求/响应。

首先安装上述任一项,然后使用网络浏览器登录网站,并从网络浏览器中捕获“请求的数据”。

然后根据该数据创建HttpWebRequest

如果您的网站使用“HTTP GET”方法,则使用Alex85的方法。

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

您可以尝试使用“HTTP POST”方法的代码。

 Dim Request As HttpWebRequest
 Dim response As Net.HttpWebResponse
 Dim cookieJar As New Net.CookieContainer()

 Dim strURL As String = "http://localhost/login.php"
 dim PostData as string
 PostData = "login=root&password=root" 'Set this according to captured data

 request = Net.HttpWebRequest.Create(strURL)
 Request.Host = "localhost"
 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

Response.Close()

    Request = Net.HttpWebRequest.Create(strURL)
    Request.Host = "localhost"
    Request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"
    Request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
    Request.KeepAlive = True
    Request.CookieContainer = CookieJar
    Request.AllowAutoRedirect = False
    Request.ContentType = "application/x-www-form-urlencoded"
    Request.Method = "POST"
    Request.ContentLength = PostData.Length

    Dim requestStream As Stream = Request.GetRequestStream()
    Dim postBytes As Byte() = Encoding.ASCII.GetBytes(PostData)

    requestStream.Write(postBytes, 0, postBytes.Length)
    requestStream.Close()

    Dim Response As HttpWebResponse = Request.GetResponse()
    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