0

I tried program to login to warezbb, somehow i think it doesn't return or idk what the problem, help me out , even if i entered the correct login details it still came out "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

image link

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • @csharpler this is not too important but is kind of weird: I was the one performing the editing ("'From the code in the provided picture, you missed this line" was written by me) and you are the one appearing as the editor?! – varocarbas Jul 14 '13 at 08:28
  • @varocarbas: Your edit was [rejected](http://stackoverflow.com/review/suggested-edits/2505010) (not by me but 3 others), but I think it is correct so I inserted the line. – pascalhein Jul 14 '13 at 08:30
  • @csharpler Ah!? OK, as said, no big deal (at all). But the summary is that my correction was rejected, you wrote the same and yours wasn't?! Don't really care because I am not an editor (just do that when I see a clear error and I am the first one answering the question) but pretty curious situation. Anyway... Aaron Campin sorry for writing comments not referring directly to your question; you can check my answer and comment anything there. – varocarbas Jul 14 '13 at 08:34
  • @varocarbas: If you have [2000 reputations](http://stackoverflow.com/help/privileges/edit), you may edit posts without review. This is why nobody rejected my edit. – pascalhein Jul 14 '13 at 08:35
  • 1
    OK @csharpler thanks for the info. I look forward the day when I will get this super-power :) – varocarbas Jul 14 '13 at 08:36

1 Answers1

1

The error you are getting is quite descriptive: you cannot create a function where you haven't accounted for all the possible options to return a value. In your code you are missing the else to the condition If InStr(ServerRespond, "You have successfully logged in") Then (you should write, else return False).

The best way to avoid this kind of problems is setting a return statement at the end of the function taking care of any situation not accounted for by the code above. Example:

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
varocarbas
  • 12,354
  • 4
  • 26
  • 37
  • thank you so much for the reply, the error is fixed, but I still can't get it working, lol, I'm new at programming, but thank you anyway :) seems like the program didn't even log in to the specified page –  Jul 14 '13 at 09:37
  • Yes, from you question this was pretty clear to me :). Sorry for not being in a position to help here but I don't have any experience in setting up VB.NET applications logging into websites. You should post a new question about this specific problem. – varocarbas Jul 14 '13 at 09:40