Results 1 to 12 of 12
  1. #1
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,682
    My Mood
    Mellow

    [Tutorial] Using HttpWebRequests to browse forums.

    Tutorial: Using HttpWebRequests
    Written by Jason of MPGH. None of this tutorial was taken from any other site. Do not post this tutorial elsewhere.

    Hey guys, someone asked about simulating a forum login and navigation with HttpWebRequests. Midway through replying I realized I may as well just post a tut about it.

    What you need:
    My web scraping toolkit: https://www.mpgh.net/forum/33-visual-...s-library.html
    LiveHTTPHeaders (Just google it, it's the first result)
    A brain that works.
    Fundamental VB knowledge: (where class-level declarations go, where Imports go)

    Alright, now that you have the necessary equipment, it's time to start the coding.

    First thing is first, we needa set up which namespaces to import and what class-level variables to declare. We need to add the WebClasses.dll as a reference before any of that, so go Project->Add Reference and then browse to wherever you saved my .dll.

    Once that is referenced we need to import the WebClasses namespace, as well as the System.Net namespace. After the imports we'll make a class-level new instance of the WebClass class as well as a new CookieContainer.
    See below:

    [highlight=vb.net]
    Imports WebClasses
    Imports System.Net

    Public Class Form1 '<< That is irrelevant, it will be the name of your current form so whatever it is, don't change it...this is just to show where to import/declare
    Private wClass As New WebClass()
    Private cCont As New CookieContainer()
    [/highlight]

    Now, you're going to need to get some info about the site so that you can login! It's time to get LiveHTTPHeaders crankin' (I'll be

    calling LiveHTTPHeaders "LHH" from now on 'cos cbf typing).

    Crash course on getting the info:
    • Step 1: Open up LHH in Firefox, uncheck the capture button and press "clear" if there is any text in there already
    • Step 2: Navigate firefox to your forum of choice (on the login page)
    • Step 3: Enter your forum credentials into the login box but DO NOT PRESS SUBMIT YET.
    • Step 3.1: [troll]email me your forum credentials[/troll]
    • Step 4: [NOTE: STEP 4+5 ACHIEVE BEST RESULTS WHEN DONE QUICKLY, SO READ BOTH BEFORE PROCEEDING] Go back to LHH and check the 'capture' box
    • Step 5: Swap back to the forum and click the login button
    • Step 6: As soon as the next page of the forum loads, go back to LHH and uncheck the 'capture'
    • Step 7: Scroll right to the top of LHH, if you did it quickly enough, the first or second entry should be from the forum and the METHOD should be "POST"
    • Step 8: Leave LHH open on that entry for now, we'll go back to it soon.


    Okay, back to coding.

    Now, we must begin our login method.

    [highlight=vb.net]
    Private Function tryLogin(ByVal username As String, ByVal password As String) As Boolean
    End Function
    [/highlight]

    There's our function declaration. It's a function because we're going to 'try' and login (will return true if the login was successful, false otherwise)

    Now to start filling out that function. Primarily we're going to be utilizing the postData function of the WebClass class, so we'll start working away at the fields required to construct that function.

    Parameter One: SiteToPost
    This is by far the easiest, go to LHH and the very first line of the entry will be the website link, right click it and select copy. for

    the purposes of this tutorial my site will be "https://www.example.com/forum/login.php?do=login"
    In vb:
    [highlight=vb.net]
    Dim site As String = "https://www.example.com/forum/login.php?do=login"
    [/highlight]

    Parameter Two: PostString
    This one is the hardest one. Go to LHH, about midway down the entry there should be a line called "Content-Length" with an indented line beneath it. The indented line is the one we want, it contains all the info that was posted to the site. Depending on the forum, this can be a huge issue (some sites use random salts, some have strange encryptions...etc so yeah, this isn't a guaranteed fail-safe method. Either way, right click->copy the indented line.

    For this example we'll say my indented line was simply "username=Jason&password=trololol", now we need to make this dynamic so other people can use their credentials, for your own you're going to have to search through the string till you see the credentials you used, then remove them and input the 'username' and 'password' variables as shown below
    [highlight=vb.net]
    Dim data As String = "username=" & username & "&password=" & password
    [/highlight]

    This means that your data string will depend on the credentials the user input.

    Parameter Three Referer
    This is optional, you can find the Referer in LHH, so if you want to set it..you can. All the referer does is basically describes what page directed you to the login page. I.e if you try to post on some forums without being logged in, that page will REFER you to the login page, once you login the site processes where you were sent from originally and puts you back there, that's basically what referer does.
    [highlight=vb.net]
    Dim referer As String = "" 'I'm not going to have a referer so I shall leave this blank.
    [/highlight]

    Parameter Four: Cookies
    This is your cookiecontainer, you need this if you want to browse around the forum after you login with all your 'logged-in' priviledges. Remember that class-level CookieContainer we made before? That's what we'll use it for.

    Okay, it's time to put that all together, with the postdata function included!

    [highlight=vb.net]
    Private Function tryLogin(ByVal username As String, ByVal password As String) As Boolean
    Dim site As String = "https://www.example.com/forum/login.php?do=login"
    Dim data As String = "username=" & username & "&password=" & password
    Dim referer As String = ""

    Dim requestString As String = wClass.postData(site, data, referer, cCont)
    End Function
    [/highlight]

    You may be wondering why I declared requestString and why postData returns a string, this is to help you authenticate your login, the return string is the page source of the response from the website, this is how you validate the login: you test the resulting string to see if certain phrases or whatever are in there. For example, on VBulletin forums when you log in it comes up with the "thank you for logging in, Jason", so I'm going to test that here to validate the login

    [highlight=vb.net]
    Private Function tryLogin(ByVal username As String, ByVal password As String) As Boolean
    Dim site As String = "https://www.example.com/forum/login.php?do=login"
    Dim data As String = "username=" & username & "&password=" & password
    Dim referer As String = ""

    Dim requestString As String = wClass.postData(site, data, referer, cCont)
    Dim retVal As Boolean = False 'default return is false
    If requestString.Contains("Thank you for logging in,") Then retVal = True 'if the login was successful, change retVal to true

    Return retVal 'return the result.
    End Function
    [/highlight]

    So now you can test if you logged in correctly

    [highlight=vb.net]
    Dim loggedIn As Boolean = tryLogin("Jason", "trololol")
    If loggedIn
    MessageBox.Show("You were successfully logged in buddy! ")
    Else
    MessageBox.Show("Sorry, you were not logged in, please try again")
    End If
    [/highlight]

    And there you have it. Because the CookieContainer was passed ByRef(erence) to the postData function, it will be full to the brim of Cookie goodness. After this, you can just make use of the CookieContainer and the GetPageSource function of the WebClass class to make requests to various pages on the forum.

    [highlight=vb.net]
    Dim forumSrc As String = wClass.GetPageSource("https://www.example.com/forum/33-visual-basic/", cCont)
    [/highlight]

    That is all from this tutorial, I hope someone get something out of this. It is a very handy skill to have.

    Cheers.
    Jason.

    PS. Please note that I wrote all of this in notepad (including the code) so if there are any coding errors, don't flame me...just point them out, I'm bound to have made a syntax error somewhere.
    Last edited by Jason; 03-31-2011 at 01:41 AM. Reason: syntax /rage

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  2. The Following 8 Users Say Thank You to Jason For This Useful Post:

    'Bruno (03-31-2011),Blubb1337 (03-31-2011),cgallagher21 (03-28-2015),Hassan (03-30-2011),Lyoto Machida (03-31-2011),MultiPlex (03-30-2011),nightdemon1 (03-31-2011),Qizzle15401 (04-06-2011)

  3. #2
    Lawllypawp's Avatar
    Join Date
    Feb 2011
    Gender
    female
    Location
    Melbourne, Australia
    Posts
    582
    Reputation
    27
    Thanks
    66
    My Mood
    Sick
    Would take 10 second's if you know what you are doing.

  4. #3
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,682
    My Mood
    Mellow
    Quote Originally Posted by Lawllypawp View Post
    Would take 10 second's if you know what you are doing.
    Um...your point?

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  5. #4
    justiman's Avatar
    Join Date
    Nov 2010
    Gender
    male
    Posts
    47
    Reputation
    10
    Thanks
    4
    My Mood
    Fine
    thanks for writing this for me

  6. #5
    Lyoto Machida's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Location
    Far away with girls
    Posts
    3,734
    Reputation
    133
    Thanks
    1,621
    My Mood
    Aggressive
    Quote Originally Posted by justiman View Post
    thanks for writing this for me
    not for you So gtfu

    It was for me /me /joking xD

    Thanks Jason ^^ , But still using a webbrowser i think, The web scrap thing xD
    Last edited by Lyoto Machida; 03-30-2011 at 01:30 PM.

  7. #6
    justiman's Avatar
    Join Date
    Nov 2010
    Gender
    male
    Posts
    47
    Reputation
    10
    Thanks
    4
    My Mood
    Fine
    Quote Originally Posted by -Away View Post

    not for you So gtfu

    It was for me /me /joking xD

    Thanks Jason ^^ , But still using a webbrowser i think, The web scrap thing xD
    no it actually was for me jason posted it in my http wrapper thread

  8. #7
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,682
    My Mood
    Mellow
    Quote Originally Posted by -Away View Post

    not for you So gtfu

    It was for me /me /joking xD

    Thanks Jason ^^ , But still using a webbrowser i think, The web scrap thing xD
    It was for justiman by the way.

    You should NEVER have to use a fagbrowser for simple scraping.

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  9. #8
    VernK's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    49
    Reputation
    10
    Thanks
    47
    My Mood
    Relaxed
    Good job Jason this could be usefull! I would rather go with fagbrowser though just becuase I'm a fag user.
    I am Here to Program!
    __________________________________________________
    Open Source Project: [Advanced IP Tool][Released]
    Open Source Project: [Advanced IP Tracker][Released]
    Download the project here! - IP Tool v1.5 By VernK - Full Source
    __________________________________________________
    Visual Basic: 67%
    Visual C#: 23%
    Visual Web Dev: 10%
    Visual C++: 5%


    Feel free to ask me any questions or get help:
    Skype: veeran.kerai. rocks (Active) <remove space>
    AIM: veerank12@aol.com
    YAHOO: veerank12@yahoo.com
    MSN: veerank12@live.com (Very Active)
    GMAIL: movemanb@gmail.com
    E-MAILS: veerank12@mail.com
    veerank12@gm*****m
    Or PM Me on MPGH!

  10. #9
    ³²³'s Avatar
    Join Date
    Mar 2011
    Gender
    male
    Location
    sef
    Posts
    302
    Reputation
    9
    Thanks
    19
    My Mood
    Angelic
    ^fagbrowser?
    webbrowser.

    Even though they pretty much fail completely at what you need to do in this instance they aren't bad for grabbing information and submitting forms SIMPLE-ly. (sic)

  11. #10
    MultiPlex's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    2
    Reputation
    10
    Thanks
    0
    Great Tutorial. I don't believe the noobs still persisting to use webbrowsers.

  12. #11
    Blubb1337's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Location
    Germany
    Posts
    5,915
    Reputation
    161
    Thanks
    3,109
    Thanks, can come in useful



  13. #12
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,682
    My Mood
    Mellow
    Thanks for the comments guys.

    WebBrowsers are a really inefficient and inaccurate way of doing any type of web-scraping. The only time they are a feasible alternative is when you can't simulate the login yourself (due to lack of knowledge about the various encryptions the site uses on its postString...or other factors such as general laziness)

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)