1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

any vb.net login codes?

Discussion in 'Visual Basic .NET' started by voidale, Mar 5, 2010.

  1. voidale

    voidale Jr. VIP Jr. VIP Premium Member

    Joined:
    Sep 29, 2008
    Messages:
    583
    Likes Received:
    176
    Hey, I'm trying to create a bot using vb.net for the first time, but I can't make documentcompleted work, I'm trying to login to a website wait for the browser to load and then navigate to another page but what i get it skips the login and navigate to second page.
    here is the code i got so far
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            WebBrowser1.Document.GetElementById("nickname").SetAttribute("value", "user")
            WebBrowser1.Document.GetElementById("password").SetAttribute("value", "pass")
            WebBrowser1.Document.GetElementById("Submit").InvokeMember("Click")
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            WebBrowser1.Navigate("http://website.com")
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            WebBrowser1.Navigate("http://website.com")
        End Sub
    
        Private Sub if WebBrowser1_DocumentCompleted(ByVal sender As System.Object, _
                                                ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
                                                Handles WebBrowser1.DocumentCompleted then
            WebBrowser1.Navigate("http://website.com/page2/")
            End if
    
        End Sub
    End Class
    anyone have an idea how to make it work or a vb.net login source code? :)
     
  2. mline

    mline Newbie

    Joined:
    Jan 30, 2010
    Messages:
    49
    Likes Received:
    18
    Honestly, I highly suggest using a different method of requesting pages. If you want something that's as easy (if not more easy) than using a WebBrowser control I would suggest using HttpWebRequest. This allows you to access webpages in a more "socket-like" sort of way with all the ease of a webbrowser control. There are limitations of course (though far less than with a webbrowser control). You can use a synchronous architecture for it, as I show below or you can go asynchronous.

    Code:
    Imports System.Net
    Imports System.IO
    
    Dim hReq As HttpWebRequest
    Dim hRes as HttpWebResponse
    
    'Setup request
    hReq = HttpWebRequest.Create("http://www.webpage.com")
    
    'Get Response
    hRes = hReq.GetResponse()
    
    Dim Buffer(4096) As Byte, BlockSize As Integer  
      
    'Memory stream to store data  
    Dim TempStream As New MemoryStream  
    Do  
        BlockSize = SourceStream.Read(Buffer, 0, 4096)  
        If BlockSize > 0 Then TempStream.Write(Buffer, 0, BlockSize)  
    Loop While BlockSize > 0 
    
    htmlData = TempStream.ToString
    
    That is just from memory mostly. There are better ways to work with it. I just wanted to give you the basic idea. I havent used it since i first converted to vb.net back in '04. Eventually you'll want to create your own http class which creates handling for raw socket data from http requests.

    Check out these articles if you're interested in HttpWebRequest, it really is easy and a million times better than the webbrowser control.

    http://www.vbdotnetheaven.com/UploadFile/mahesh/DownloadWebPage04252005073432AM/DownloadWebPage.aspx

    http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

    http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.aspx

    I may be able to find some old source that uses it if you really need it.
     
    • Thanks Thanks x 1
    Last edited: Mar 6, 2010
  3. jangedoo

    jangedoo Registered Member

    Joined:
    Jan 12, 2010
    Messages:
    59
    Likes Received:
    41
    Are you sure there is an "IF" on the function "WebBrowser1_DocumentCompleted" ???

    From my understanding you are trying to Login and then maybe lets say goto the profile page.
    What I would do is set up a boolean variable that would store whether I'm "loggedin= TRUE" or not.
    If I'm logged in or loggedin=TRUE then I would goto the profile page else I wud try to login again.

    I hope you understood
     
  4. Monrox

    Monrox Power Member

    Joined:
    Apr 9, 2010
    Messages:
    615
    Likes Received:
    579
    The suggestions above are good and once you get the hang of it you'll see it's better to leave the webbrowser control alltogether.

    To make your code work first you tell the browser to go to a page. You wait for the DocumentCompleted event to fire, then you enter your login info and click the login button. It will navigate to the next page automatically (because you are clicking the button on the page).


    Code:
     
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            WebBrowser1.Navigate("[URL="http://website.com/"]http://website.com[/URL]")
        End Sub
     
     
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
        Handles WebBrowser1.DocumentCompleted
     
            If WebBrowser1.ReadyState = WebBrowserReadyState.Complete = True Then
     
                WebBrowser1.Document.GetElementById("nickname").SetAttribute("value", "user")
                WebBrowser1.Document.GetElementById("password").SetAttribute("value", "pass")
     
                'assuming your elements are correct, the webbrowser will navigate automatically after clicking the button
                WebBrowser1.Document.GetElementById("Submit").InvokeMember("Click")
     
            End If
     
        End Sub
    
     
    Last edited: Oct 20, 2010