0

我尝试程序登录到warezbb,不知何故我认为它没有返回或idk是什么问题,请帮助我,即使我输入了正确的登录详细信息它仍然出现“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 回答