C# Post Login Tutorial For MPGH (For WebScraping)

Posts 115 of 15 · Page 1 of 1
C# Post Login Tutorial For MPGH (For WebScraping)
Hello, This is my first tutorial so please give any constructive Criticism.

Today, I am going to teach how to log into the Site using the HttpWebRequest method and Post data to the php script.

So lets start


Step 1:
__________________________________________________ ____________________________________
First Open Visual C# 2010 and create a windows form application
Create a Form with 2 text boxes and a button:
Like So:

For Tutorial Purpose name textBox1 to Username and textBox2 to Password, and Button1 to btnLogin
Now that the layout is designed lets start Step 2
__________________________________________________ ____________________________________

Step 2:
__________________________________________________ ____________________________________
Now Double Click on the Form and it will open a popup used for coding.
The first thing we need to do is declare what we will be using.
Add this to the using statements.
Code:
using System.Security.Cryptography;
using System.Net;
using System****;
So in all it should look like
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Net;
using System****;

namespace WindowsFormsApplication1
Now that the importing is done we can start on the actual coding.
__________________________________________________ __________________________________

Step 3
__________________________________________________ __________________________________
First Create a method named Login which takes the parameters of two strings and returns a Boolean.
Code:
public Boolean Login(String Username, String Password)
{

}
Now lets add the starting variables

Code:
 Password = MD5(Password); //This is actually another Custom Method, so for now please do not worry about this as It will be later explained
            Boolean Value = false; //The Boolean we are returning
            String Data = "vb_login_username=" + Username + "&vb_login_password=&s=&do=login&vb_login_md5password=" + Password + "&vb_login_md5password_utf=" + Password; //This is for all vBulletins, as this is a coding tutorial I will not explain this.
Now that the Starting Variables are created, we can create the HttpWebRequest, we will use the try and catch method for this.
Code:
try
            {
                HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("http://www.mpgh.net/forum/login.php?do=login"); //Creates a httpwebrequest to post to the login.php
                Request.Method = WebRequestMethods.Http.Post; //using Post Method to php script
                Request.ContentType = "application/x-www-form-urlencoded"; //Vbulletin method (Gotten from Research)
                Request.UserAgent = "-- vBulletin Vaidation  --"; /Vbulletin Validation (Gotten from Research)

                Request.ContentLength = Data.Length; //Gets the Post Request length to the same as the data we are posting.

                StreamWriter rStream = new StreamWriter(Request.GetRequestStream()); //Write Data to the stream
                rStream.Write(Data);
                rStream.Flush(); 
                rStream.Close();

                HttpWebResponse Response = (HttpWebResponse)Request.GetResponse(); //After writing data to the stream get the response

                StreamReader resReader = new StreamReader(Response.GetResponseStream()); //Convert to response to string

                String str = resReader.ReadToEnd(); //Read contents of the whole string (Which is the same if you view source of php script

                if (str.Contains("Thank you")) //Read whole string to see if Thank You was processed, as thats what it says when you log in
                {
                    Value = true; //Set the Returned Boolean to true,
                    MessageBox.Show("LOGIN!!"); //Prove that you were logged in
                }
                else
                {
                    Value = false; //Return Boolean to False
                    MessageBox.Show("FAIL!!"); //Prove that incorrect emails.
                }

                Response.Close(); //Close the Response
            }
            catch (Exception ex) //If there is an error, display what was wrong with a message box named MPGH Login.
            {
                MessageBox.Show(ex.Message, "ERROR MPGH LOGIN!!");
            }

            return Value;
The code is all commented, This is how to do a try and catch method using HTTPRequestMethod, That's it for logging in and posting data.
__________________________________________________ __________________________________________________ ________________

Step 4:
__________________________________________________ __________________________________________________ ________________
Remember the Custom MD5(Password) we had, we now need to declare that, and create it appropriately, and return the string
Code:
        public String MD5(String Pass)
        {
            ASCIIEncoding ASCIIenc = new ASCIIEncoding(); //Create new ASCIIEncoding
            String strReturn = String.Empty; //Create New String
            Byte[] ByteSourceText = ASCIIenc.GetBytes(Pass); //Create New Byte Array that contains the Bytes of the passed password
            MD5CryptoServiceProvider Md5Hash = new MD5CryptoServiceProvider(); //Create a new MD5 Hash Encryption
            Byte[] ByteHash = Md5Hash.ComputeHash(ByteSourceText); //Create another Byte Array which contains Encrypted Bytes of the Passed password

            foreach(Byte b in ByteHash){ //Get every byte in the Encrypted Array.
                strReturn += b.ToString("x2"); //Add it all to the string with the following method
            }



            return strReturn; //return the Md5 encrypted password for the login process
        }
That is all for the MD5 Encryption Custom Method
__________________________________________________ _______________________________________________

Step 5
__________________________________________________ _______________________________________________
Lets create the listener on the login button,
Code:
        private void btnLogin_Click(object sender, EventArgs e) //btn is clicked
        {
            Login(Username.Text, Password.Text); //Pass the Contents of the login boxes Username and Password to the custom Method
        }
Now to do that is the end, the rest is up to you.
Nice Job Rakuuza, Keep it Up!
Quote Originally Posted by user590177 View Post
Well done bro.
Thanks bro
Just seen this. Posted an example in other thread. Oh well. Waste of 30 mins I guess. Little messy but good work.
Quote Originally Posted by tlokzz View Post
Just seen this. Posted an example in other thread. Oh well. Waste of 30 mins I guess. Little messy but good work.
Your example was still a great help. Thank-you!
Quote Originally Posted by Rakuuza View Post
Your example was still a great help. Thank-you!
Sorry for just coming here with this but:

If he was of help to you with his source/tutorial.
Maybe you should credit him in your tutorial ?

Since it's kinda unfair to not be given credits when your source/tutorial was to great help.
Like I would copy and paste most of this, and say I made it.

And you saw the source and it was exact same.
THink you might have felt it a bit "irritating". That you used time to make it, and I just C&P and say I made it...
Just saying, give credits don't make your work look bad, or your skills.
It's even better to give credits. Because if you don't and someone ask you for help.
You might not know what to answer since you have no clue what was done...

Just saying.
If you find this somehow offending or so. Let me know.
Also, just VM/PM me if you have anything to say about it.
Quote Originally Posted by Jorndel View Post
Sorry for just coming here with this but:

If he was of help to you with his source/tutorial.
Maybe you should credit him in your tutorial ?

Since it's kinda unfair to not be given credits when your source/tutorial was to great help.
Like I would copy and paste most of this, and say I made it.

And you saw the source and it was exact same.
THink you might have felt it a bit "irritating". That you used time to make it, and I just C&P and say I made it...
Just saying, give credits don't make your work look bad, or your skills.
It's even better to give credits. Because if you don't and someone ask you for help.
You might not know what to answer since you have no clue what was done...

Just saying.
If you find this somehow offending or so. Let me know.
Also, just VM/PM me if you have anything to say about it.
During your life you will learn a lot! Let me give you an example....When u start learning a programming language you will start with the basics and some articles, but it's not like you are going to credit the author of the article or of the book that you are reading..
Quote Originally Posted by Rakuuza View Post
Excuse me, I understand what you are getting at but my source was posted before his, not only that I have not edited this post since seeing his source. His source taught me how to do it in a cleaner method, but in know way did his source contribute to this source. Only to my knowledge

If you check the dates of posts you will see that I am not lying.

If I was to post another source after learning him I would say thank you for the improvements. And give some sort of acknowledgement

So please next time please refrain from criticism when it wasn't due.

Sorry if I come across rude! I don't mean to!
Oki, how could I know
Since you used: was which made me think, he WAS to help :P
(I am not really looking at people who is right and wrong. I just wanted to share what I have been teached...)




Quote Originally Posted by user590177 View Post
During your life you will learn a lot! Let me give you an example....When u start learning a programming language you will start with the basics and some articles, but it's not like you are going to credit the author of the article or of the book that you are reading..
Found it


What I was given during my law and order class :S
(So, I find it a bit important to say that someone helped you if they did.)

Last post from me here
(Don't want to go off-topic, what is starting now :P)
Excuse me, I understand what you are getting at but my source was posted before his, not only that I have not edited this post since seeing his source. His source taught me how to do it in a cleaner method, but in know way did his source contribute to this source. Only to my knowledge

If you check the dates of posts you will see that I am not lying.

If I was to post another source after learning him I would say thank you for the improvements. And give some sort of acknowledgement

So please next time please refrain from criticism when it wasn't due.

Sorry if I come across rude! I don't mean to!
All is well. I was checking out your code a little more to see if I could give you some criticism (barely read first line but kind of knew).

Well, I'm pretty sure you know this, but you must save the cookie response and add to the WebClient's header to stay "logged in".
It is a nice tutorial, but its very easy to patch it, even its crypted .
you forgot to add a slash beside the slash

Request.UserAgent = "-- vBulletin Vaidation --"; //Vbulletin Validation (Gotten from Research)
thanks works fine Thank you very much
Posts 115 of 15 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?