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

really a nice post. can’t wait to try myself. thanks for making it sound/appear so simple and straight forward.
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.
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.
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)
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!
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
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…
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
trying to post on this page
Able to login on facebook but when I am trying the same code on any other website it is not working. Please help
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
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.
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