2012-01-28 47 views
0

我是一個新手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>&nbsp;</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 

感謝您的幫助

回答

1

這種方法只適用於它們使用URL參數網站。確保您可以登錄到您的網站是這樣的:

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

還要去掉空格的位置:

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

確保您發送正確HttpWebRequest

可以使用Live HTTP Headers plugin for FirefoxFiddler捕捉網絡請求/響應。

首先安裝上述任意一種,然後使用Web瀏覽器登錄到網站並從Web瀏覽器捕獲「請求的數據」。

然後根據這些數據創建您的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