This blog serves as a place for me where I can occasionally dump bits of content from my brain. Feel free to browse.

Login to website using VB.NET

The example below uses HttpWebRequest and HttpWebResponse classes to log in to Facebook.

Dim cookieJar As New Net.CookieContainer()
Dim request As Net.HttpWebRequest
Dim response As Net.HttpWebResponse
Dim strURL As String
 
Try
     'Get Cookies
     strURL = "https://login.facebook.com/login.php"
     request = Net.HttpWebRequest.Create(strURL)
     request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"
     request.Method = "GET"
     request.CookieContainer = cookieJar
     response = request.GetResponse()
 
     For Each tempCookie As Net.Cookie In response.Cookies
            cookieJar.Add(tempCookie)
     Next
 
     'Send the post data now
     request = Net.HttpWebRequest.Create(strURL)
     request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"
     request.Method = "POST"
     request.AllowAutoRedirect = True
     request.CookieContainer = cookieJar
 
     Dim writer As StreamWriter = New StreamWriter(request.GetRequestStream())
     writer.Write("email=username&pass=password")
     writer.Close()
     response = request.GetResponse()
 
     'Get the data from the page
     Dim stream As StreamReader = New StreamReader(response.GetResponseStream())
     Dim data As String = stream.ReadToEnd()
     response.Close()
 
     If data.Contains("<title>Facebook") = True Then
            'LOGGED IN SUCCESSFULLY
     End If
 
Catch ex As Exception
     MsgBox(ex.Message)
End Try

Now, here is some explanation:

Get Cookies

The following block of code gets all the cookies from Facebook and stores them in a collection. These cookies are returned upon visiting the webpage: https://login.facebook.com/login.php. Getting the cookies is very important, because they must be passed back to the webpage when you are going to login.

For Each tempCookie As Net.Cookie In response.Cookies
     cookieJar.Add(tempCookie)
Next

Passing post data to the website

Next, I created a StreamWriter class and wrote the post data to it. You can pass any data to the website here in name=value&name=value format.

Dim writer As StreamWriter = New StreamWriter(request.GetRequestStream())
writer.Write("email=username&pass=password")
writer.Close()

Finally…

The following code gets the source code of the webpage returned which is the homepage of your account (i.e home.php), and checks if you are logged in successfully or not.

Dim stream As StreamReader = New StreamReader(response.GetResponseStream())
Dim data As String = stream.ReadToEnd()
response.Close()
 
If data.Contains("<title>Facebook") = True Then
     'LOGGED IN SUCCESSFULLY
End If

I hope this simple example will help you save some time. If you find it useful then do let me know via your comments.

Happy Coding :)


13 Comments on “Login to website using VB.NET”

  1. 1 IC said at 11:29 am on August 16th, 2010:

    really a nice post. can’t wait to try myself. thanks for making it sound/appear so simple and straight forward.

  2. 2 Noel R said at 6:04 am on December 6th, 2010:

    Thank you! I was having trouble because I was immediately trying to send login info without getting cookies first. Didn’t think it mattered, but it does. :)

  3. 3 Ajmal Hussain Abbasi said at 9:09 am on December 20th, 2010:

    Good post. Quite helpful at first step but can you please extend it further to let us know how can we add posts to Facebook using our VB.Net code. The above code just brings in the source code of the home page.

  4. 4 hussain said at 4:15 pm on December 22nd, 2010:

    nice forum hassan,
    i have a similar windows appllication, where i have to login to website.
    I have a question???
    You are taking tag value. Instead , i want to select a anchor tag value to compare login…
    <a href=”index.php?menu=insertTower” rel=”nofollow”>abc</a>
    this <a> is inside many tags
    How to get inner <a>tag value to compare login
    (thanks in advance)

  5. 5 admin said at 1:38 pm on December 23rd, 2010:

    Hello hussain,

    You have to use regular expressions for this purpose. But if this link <a href=”index.php?menu=insertTower” rel=”nofollow”> is unique then you can still use .contains method of the string.
    For an example of using RegEx objects in VB .NET, check this link:
    http://visualbasic.about.com/od/usingvbnet/a/RegExNET_2.htm

    I don’t know the exact regular expression pattern for anchor links though, you can try finding it yourself.

    Hope it helps you!

  6. 6 Vergil Tan said at 10:35 am on January 5th, 2011:

    This is a great article, i successfully login, but when i trying to show http://www.facebook.com on component, microsoft web browser, but it always show me the page without login, how to use this code, to login and show the profile page?

    thank you

  7. 7 __azteca__ said at 11:51 pm on April 15th, 2011:

    nice tutorial bro, but when i tried to apply on yahoo, login cant connect. i cant send data passwords and user but i m still trying. ty bro…

  8. 8 marif said at 5:55 pm on May 1st, 2011:

    HI hasan bro,

    I need to know how i can make the application of facebook to upload photos to facebook one by one to many accounts of facebook. can you help me little bro my email address is vishnuduttc@ymail.com.

    Thanks

  9. 9 tienhop said at 2:26 pm on June 23rd, 2011:

    trying to post on this page

  10. 10 stuti said at 3:52 am on July 22nd, 2011:

    Able to login on facebook but when I am trying the same code on any other website it is not working. Please help

  11. 11 vishal said at 3:23 am on September 17th, 2011:

    HI,
    can u please help me to understand ur code can u please make a project file in vb 208 and give me so i can understand it better, my email is vishnuduttc@ymail.com

  12. 12 Thomas said at 3:05 am on November 26th, 2011:

    I am attempting to sign into Dailymotion.com using proxy then posting a comment to specific video url all done with post/get http.

    I am willing to pay you to help me with this. This is very important. Can we talk about a price that will be fair for you. Please email me.

  13. 13 Mattias said at 3:08 am on November 29th, 2011:

    I love the code but i need to know how to post on my wall or on the home page using this http system.

    // Mattias


Leave a Reply