我是一個新手vb.net(visual studio 2008)。我試圖做一個應用程序使用vb.net可以用於登錄到網站和瀏覽網站沒有使用webbrowser(我不想使用vb.net的webbrowser)。我從網上獲得了一個代碼;我已經使用php和mysql在我的電腦(其工作正常)臨時登錄網頁。 但是當我試圖登錄使用vb.net它不工作... 因爲我不知道哪部分代碼不工作,我在這裏粘貼整個代碼。不能登錄網站使用httpwebrequest/responce 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> </td>
<td><input name="submit" value="Login" class="button" type="submit"></td>
這是vb.net代碼,我是從net.i改變URL以我的本地website..and添加用戶名和密碼(包括根)也該<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
感謝您的幫助