Hello There, Guest!
View New Posts  |  View Today's Posts
[VB.Net] Login to a Website using WebRequests.

ii 
  • 4 Vote(s) - 5 Average


06-05-2011, 11:50 PM #1
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

Login to a Website using WebRequests.
Per Smed's request, I am going to do a quick tutorial on logging into websites using WebRequests.

You will need:
  • Live HTTP Headers (Firefox addon)
  • Visual Studio

I am going to show you how to log into this site, TechLifeForum.net.

First, open Live HTTP Headers (under the Tools Menu). Then while logged out, enter your account information and log in. You will see something like this:


The line we will be most concerned with is:

[spoiler=Edit: Since changing themes, the PostData line is different.]
Old:
Code:
action=do_login&url=http%3A%2F%2Ftechlifeforum.net%2Findex.php&quick_login=1&quick_username=kobe&quick_password=removed&submit=Login&quick_remember=yes
New:
Code:
username=kobe&password=removed&remember=yes&submit=Login&action=do_login&url=http%3A%2F%2Ftechlifeforum.net%2Findex.php
[/spoiler]


Now start up Visual Studio and create a new project.
Add the following controls:
  • (2) textboxes: txtUser, txtPass
  • (1) Button: btnLogin

Add the following imports:
Code:
Imports System.IO, System.Net, System.Text

Put this under Public Class Form1
Code:
Dim mainCookie As New CookieContainer

Put this code in your btnLogin_Click() event
Code:
Dim postData As String = "action=do_login&url=http%3A%2F%2Ftechlifeforum.net%2Findex.php&quick_login=1&quick_username=" & txtUser.Text & "&quick_password=" & txtPass.Text & "&submit=Login&quick_remember=yes"
        Dim bytes() As Byte = ASCIIEncoding.UTF8.GetBytes(postData)
        Dim postReq As HttpWebRequest = WebRequest.Create("http://techlifeforum.net/member.php")
        postReq.Method = "POST"
        postReq.KeepAlive = True
        postReq.CookieContainer = mainCookie
        postReq.ContentType = "application/x-www-form-urlencoded"
        postReq.Referer = "http://www.techlifeforum.net/index.php"
        postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 ( .NET CLR 3.5.30729; .NET4.0E)"
        postReq.ContentLength = bytes.Length
        Dim postStream As Stream = postReq.GetRequestStream()
        postStream.Write(bytes, 0, bytes.Length)
        postStream.Close()
        Dim postResponse As HttpWebResponse
        postResponse = postReq.GetResponse()
        mainCookie.Add(postResponse.Cookies)
        Dim reader As New StreamReader(postResponse.GetResponseStream())
        Dim strSource As String = reader.ReadToEnd
        If strSource.Contains("Welcome back") Then
            MessageBox.Show("Login Successful")
        Else
            MessageBox.Show("Login Failed")
        End If

Now to explain. Recall, I said the line we will be most concerned with is this one:
Code:
action=do_login&url=http%3A%2F%2Ftechlifeforum.net%2Findex.php&quick_login=1&quick_username=kobe&quick_password=removed&submit=Login&quick_remember=yes
This is the information being sent containing our login information.

In our webrequest we set
Code:
Dim postData As String = "action=do_login&url=http%3A%2F%2Ftechlifeforum.net%2Findex.php&quick_login=1&quick_username=" & txtUser.Text & "&quick_password=" & txtPass.Text & "&submit=Login&quick_remember=yes"
I took out the username and password and put in the textbox values. This means it will send whatever data you type into the textboxes.

The very first line in the Live HTTP headers data shows the page it is posting to. We also need to set
Code:
Dim postReq As HttpWebRequest = WebRequest.Create("http://techlifeforum.net/member.php")
Where the URL is the same as the Live HTTP header line 1.

The only other line that needs set is
Code:
postReq.Referer = "http://www.techlifeforum.net/index.php"
This line comes from the Referer: line in Live HTTP Headers.

This was put together real fast so if you have any questions post them below. Also the full source with comments is below. I recommend looking through them and reading the comments. It will help you get a better understanding.

Full Source:
Code:
Imports System.IO, System.Net, System.Text
Public Class Form1
    'holds the cookies to keep you logged in
    Dim mainCookie As New CookieContainer
    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        'taken from Live HTTP headers. This is the data
        'that gets sent when logging in.
        Dim postData As String = "action=do_login&url=http%3A%2F%2Ftechlifeforum.net%2Findex.php&quick_login=1&quick_username=" & txtUser.Text & "&quick_password=" & txtPass.Text & "&submit=Login&quick_remember=yes"
        'when POSTing data, it needs to be in byte format.
        Dim bytes() As Byte = ASCIIEncoding.UTF8.GetBytes(postData)
        'this is the page we are loggin into
        Dim postReq As HttpWebRequest = WebRequest.Create("http://techlifeforum.net/member.php")
        'it's a POST request, not GET
        postReq.Method = "POST"
        postReq.KeepAlive = True
        'holds our cookies
        postReq.CookieContainer = mainCookie
        'comes from Live HTTP Headers, most likely the same for any request
        postReq.ContentType = "application/x-www-form-urlencoded"
        'from Live HTTP Heasers, some sites require the correct referer
        postReq.Referer = "http://www.techlifeforum.net/index.php"
        'standard
        postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 ( .NET CLR 3.5.30729; .NET4.0E)"
        'length of our post data
        postReq.ContentLength = bytes.Length
        'gets the stream to send data on
        Dim postStream As Stream = postReq.GetRequestStream()
        'send the data
        postStream.Write(bytes, 0, bytes.Length)
        'close the stream
        postStream.Close()
        'get the response from the website
        Dim postResponse As HttpWebResponse
        postResponse = postReq.GetResponse()
        'add the login cookies
        mainCookie.Add(postResponse.Cookies)
        'reads the response
        Dim reader As New StreamReader(postResponse.GetResponseStream())
        'puts the response into a string
        Dim strSource As String = reader.ReadToEnd
        'if it says Welcome back then it worked
        If strSource.Contains("User Profile:") Then
            MessageBox.Show("Login Successful")
        Else
            MessageBox.Show("Login Failed")
        End If
    End Sub
End Class
This post was last modified: 06-26-2012, 08:56 AM by KoBE.

06-06-2011, 05:32 AM #2
Cody8295
I code stuff.
**
Posts: 164 Threads:31 Joined: Jun 2011 Reputation: 10

RE: Login to a Website using WebRequests.
Great tutorial, thank you.
Tip me some bitcoins- 129jzwCRUPtLxfFuZnkMsXa46ao7Y4v6Ah

06-06-2011, 06:08 AM #3
Smed
Member
**
Posts: 61 Threads:6 Joined: Jun 2011 Reputation: 3

RE: Login to a Website using WebRequests.
Great tutorial 5/5
never had experience with web request but understood it all thanks.

06-06-2011, 08:51 AM #4
iNFIDEL_
Member
**
Posts: 57 Threads:11 Joined: Jun 2011 Reputation: 0

RE: Login to a Website using WebRequests.
Can this detect groups?
for example, I wanted to use this template as an Authentication to use the program and users need to be registered to myBB boards.

In order for them to use the program they must first have permission (group).

Question:
Can I make this happen that only specified 'group' of users are able to use the program?

Example: I set up "VIP" as group in myBB.
When I logged on using "TestUser" and by default it is "registered member"
Program throws back error "You can't use this app for you dont have enough privileges".
And when I logged on using "Admin" as "Admin group"
program will show a hidden form (basically the main application).

Can I do that? If yes, How?

06-06-2011, 09:53 AM #5
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

RE: Login to a Website using WebRequests.
(06-06-2011, 08:51 AM)iNFIDEL_ Wrote:  Can this detect groups?
for example, I wanted to use this template as an Authentication to use the program and users need to be registered to myBB boards.

In order for them to use the program they must first have permission (group).

Question:
Can I make this happen that only specified 'group' of users are able to use the program?

Example: I set up "VIP" as group in myBB.
When I logged on using "TestUser" and by default it is "registered member"
Program throws back error "You can't use this app for you dont have enough privileges".
And when I logged on using "Admin" as "Admin group"
program will show a hidden form (basically the main application).

Can I do that? If yes, How?

You would have to build that in but it's possible. Once logged in, you can then navigate to pages and parse the source for information. For instance, you could navigate to the VIP page, and look for something on the page that verifies access. Then use that to determine the access to your program.

06-06-2011, 12:03 PM #6
iNFIDEL_
Member
**
Posts: 57 Threads:11 Joined: Jun 2011 Reputation: 0

RE: Login to a Website using WebRequests.
Does myBB records your membership status in cookie?
as I looked in to your code, it seems to read the cookie, am I wrong?

06-06-2011, 12:54 PM #7
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

RE: Login to a Website using WebRequests.
The app doesn't read the cookie. It stores information from the site into a cookie container so that the site will remember you being logged in. This allows you to move from page to page without having to log in every time. It's the same as if you were using a web browser. In order for you app to verify VIP membership, you would need it to navigate to a page that only VIP members can access, and search for whatever error you would get if they didnt have access. If the error exists, then they are not VIP, if not. then they are VIP.

06-06-2011, 08:40 PM #8
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: Login to a Website using WebRequests.
(06-06-2011, 12:03 PM)iNFIDEL_ Wrote:  Does myBB records your membership status in cookie?
as I looked in to your code, it seems to read the cookie, am I wrong?

All your session information is stored in packets in a cookie, your online id is what they contain, so the answer would be yes, for what gets stored in a cookie.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

06-06-2011, 09:02 PM #9
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

RE: Login to a Website using WebRequests.
(06-06-2011, 08:40 PM)Unleashed Wrote:  All your session information is stored in packets in a cookie, your online id is what they contain, so the answer would be yes, for what gets stored in a cookie.

Your session information gets stored in a cookie, but not your membership such as VIP. Which I think is what he was asking. Things such as failed login attempts, session expiration, etc... get stored. Not group memberships, personal settings, etc...

06-06-2011, 09:09 PM #10
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: Login to a Website using WebRequests.
(06-06-2011, 09:02 PM)KoBE Wrote:  
(06-06-2011, 08:40 PM)Unleashed Wrote:  All your session information is stored in packets in a cookie, your online id is what they contain, so the answer would be yes, for what gets stored in a cookie.

Your session information gets stored in a cookie, but not your membership such as VIP. Which I think is what he was asking. Things such as failed login attempts, session expiration, etc... get stored. Not group memberships, personal settings, etc...

Group membership information gets stored though from a session, and actually personal settings do get stored in a cookie as preferences. I can even give you an example for showing this.
This post was last modified: 06-06-2011, 09:10 PM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲


ii 


Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
   MyBB Login Utility - Login to most MyBB websites KoBE 45 36,127 04-12-2016, 01:32 AM
Last Post: AceInfinity
   [Source] Twitter Login/Post Tweet Example (Without API, SDK or Webbrowser) idb.exe 10 11,171 06-06-2014, 11:16 PM
Last Post: hoangseo
  login to the site with get method kenshiro 3 2,354 05-19-2014, 09:58 AM
Last Post: KoBE
  How to make httpwebrequest login facebok account in listbox DilarangMasuk 3 3,255 10-05-2013, 10:51 AM
Last Post: AceInfinity
   Offline Login System (Serial generator, HWID, Expiration) [Source] KoBE 44 36,552 07-22-2013, 05:02 PM
Last Post: KoBE


Users browsing this thread: 1 Guest(s)