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

    Sep 29, 2008
    Likes Received:
    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
    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")
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        End Sub
        Private Sub if WebBrowser1_DocumentCompleted(ByVal sender As System.Object, _
                                                ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
                                                Handles WebBrowser1.DocumentCompleted then
            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

    Jan 30, 2010
    Likes Received:
    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.

    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  
        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.




    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

    Jan 12, 2010
    Likes Received:
    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

    Apr 9, 2010
    Likes Received:
    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).

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        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
            End If
        End Sub
    Last edited: Oct 20, 2010