2012-01-28 17 views
0

私はvb.net(visual studio 2008)にnewbです.iはウェブサイトにログインして閲覧するために使用できるvb.netを使用してアプリケーションを作成しようとしています(私はvb.netのwebbrowserを使いたいとは思っていません).iこのコードをネットから入手しました;私のコンピュータ上でphpとmysqlを使って一時的なログインWebページを作成しました。 しかし、私はvb.netを使用してログインしようとしたときに動作していません... コードのどの部分がうまく動作しないのか分からないので、ここでコード全体を貼り付けています。以下 httpwebrequest/responce vb.netを使用してcantログインウェブサイト

は、これは私がnet.iからそれを得た私はlocalhostのwebsite..andにURLを変更し、ユーザー名とパスワードを追加vb.netコードは(両方で

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

ログインフォームのための私のhtmlコードであります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 

ご協力いただきありがとうござい

答えて

1

このメソッドは、単にURLパラメータを使用してウェブサイトのために働きます。あなたはこのようなあなたのサイトにログインできることを確認します:

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

また、ここではスペースを削除:

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

をあなたがキャプチャしLive HTTP Headers plugin for FirefoxまたはFiddlerを使用することができます

HttpWebRequest正しいを送信していることを確認してくださいウェブリクエスト/レスポンス。

最初に上記のいずれかをインストールし、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 
関連する問題