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.
So in all it should look like
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.
Now lets add the starting variables
Now that the Starting Variables are created, we can create the HttpWebRequest, we will use the try and catch method for this.
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
That is all for the MD5 Encryption Custom Method
__________________________________________________ _______________________________________________
Step 5
__________________________________________________ _______________________________________________
Lets create the listener on the login button,
Now to do that is the end, the rest is up to you.
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****;
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
__________________________________________________ __________________________________
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)
{
}
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.
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;
__________________________________________________ __________________________________________________ ________________
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
}
__________________________________________________ _______________________________________________
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
}




.