vb.net不返回功能

时间:2013-07-14 08:08:26

标签: vb.net login return webrequest

我试过程序登录warezbb,不知怎的,我觉得它没有返回或者idk什么问题,帮帮我,即使我输入了正确的登录细节它仍然出来“其他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

image link

1 个答案:

答案 0 :(得分: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