0

warezbb にログインするプログラムを試してみましたが、どういうわけか、問題が返されないか、何が問題なのかがわからないのではないかと思います。正しいログイン情報を入力しても、「else msgbox」が出てきました

Public Class Form1 'From the code in the provided picture, you missed this line
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Login(TextBox1.Text, TextBox2.Text) = True Then
            MsgBox("you are logged in")
        Else
            MsgBox("Either password or username is incorrect , please try again")
        End If
    End Sub

    Function Login(ByVal username As String, ByVal password As String)
        Try
            Dim webRequest As HttpWebRequest

            webRequest = CType(Net.WebRequest.Create("http://www.warez-bb.org/login.php?redirect="), WebRequest)
            webRequest.Method = "POST"
            webRequest.Method = "application/x-www-form-urlencoded"""
            Dim Byte1 As Byte() = Encoding.UTF8.GetBytes("username=" & username & "&password=" & password & "&autologin=on&redirect=&login=Log+in")
            webRequest.ContentLength = Byte1.Length
            Dim Stream As Stream = webRequest.GetRequestStream
            Stream.Write(Byte1, 0, Byte1.Length)
            Stream.Close()
            Dim respond As HttpWebResponse = webRequest.GetResponse
            Stream = respond.GetResponseStream
            Dim reader As New StreamReader(Stream)
            Dim ServerRespond As String = reader.ReadToEnd
            reader.Close()
            Stream.Close()
            If InStr(ServerRespond, "You have successfully logged in") Then
                Return True
            End If
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class

画像リンク

4

1 に答える 1

1

あなたが得ているエラーは非常に説明的です.値を返すために可能なすべてのオプションを考慮していない関数を作成することはできません. あなたのコードではelse、条件にIf InStr(ServerRespond, "You have successfully logged in") Thenがありません (書く必要がありますelse return False)。

この種の問題を回避する最善の方法returnは、関数の最後にステートメントを設定して、上記のコードで説明されていない状況に対処することです。例:

Function Login(ByVal username As String, ByVal password As String)
        Dim returnedVal As Boolean = False
         Try
            Dim webRequest As HttpWebRequest

            webRequest = CType(Net.WebRequest.Create("http://www.warez-bb.org/login.php?redirect="), WebRequest)
            webRequest.Method = "POST"
            webRequest.Method = "application/x-www-form-urlencoded"""
            Dim Byte1 As Byte() = Encoding.UTF8.GetBytes("username=" & username & "&password=" & password & "&autologin=on&redirect=&login=Log+in")
            webRequest.ContentLength = Byte1.Length
            Dim Stream As Stream = webRequest.GetRequestStream
            Stream.Write(Byte1, 0, Byte1.Length)
            Stream.Close()
            Dim respond As HttpWebResponse = webRequest.GetResponse
            Stream = respond.GetResponseStream
            Dim reader As New StreamReader(Stream)
            Dim ServerRespond As String = reader.ReadToEnd
            reader.Close()
            Stream.Close()
            If InStr(ServerRespond, "You have successfully logged in") Then
                Return True
            End If
        Catch ex As Exception
            Return False
        End Try

      Return returnedVal
    End Function
于 2013-07-14T08:25:57.010 に答える