Saturday, February 28, 2009

How to log in to a website from a form (.NET, VC++2008)

Introduction
Some times you want to login to a website. Instead of using a Web browser control and having the user sign in, it would be much neater if we can just have two text boxes and a login button. Based on user request, in this tutorial we will be logging in to Facebook.

It is recommended you have some experience understanding of C++ before we continue. Also, I'm sorry about the indenting problem (Blogger seems to remove all indenting).

Step 1
Open up Visual C++ 2008 (other VC++ versions may vary slightly in code). Click File -> New Project. Type in a name for your project. Make sure you are under the "Visual C++" node and have Windows Forms Application selected.

Go ahead and click OK to create your project.

Step 2
Double click on the form (the window) to create a Load event handler for Form1. The load event handler should look something like this:
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}


Let's create a web browser that is hidden from view, but we can still use it. Let's also make it load to the homepage of Facebook.
System::Windows::Forms::WebBrowser^ m_Web;
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
m_Web = gcnew WebBrowser();
}


So, after we navigate, we want to create two text boxes. Switch back to designer view and create two text boxes.


Now we have TextBox1 and TextBox2. Using the same process, add a Button to the form. Then double-click on it.

We now have everything in place, set up and ready to go. Time for some coding.

Step 3: Code
Switch back to Form1.h using Solution Explorer or the tab at the top. We will now have to create a event handler for our web browser. After we create the event handler, we will do login error checking, then log in.
System::Windows::Forms::WebBrowser^ m_Web;
int count;

public: void myDocComplete(Object^ sender, WebBrowserDocumentCompletedEventArgs^ e)
{
if (e->Url->ToString() == "http://www.facebook.com/index.php")
{
m_Web->Document->GetElementById("email")->SetAttribute("value", textBox1->Text);
m_Web->Document->GetElementById("pass")->SetAttribute("value", textBox2->Text);

m_Web->Document->GetElementsByTagName("input")[6]->InvokeMember("click");

} else
if (e->Url->ToString() == "http://www.facebook.com/home.php?")
{
MessageBox::Show("Logged in.");
}
else
if (e->Url->ToString() == "https://login.facebook.com/login.php?login_attempt=1")
{
MessageBox::Show("Incorrect login info");
}
}

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
count = 0;
m_Web = gcnew WebBrowser();
m_Web->DocumentCompleted += gcnew WebBrowserDocumentCompletedEventHandler(this, &winform::Form1::myDocComplete);
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
m_Web->Navigate("http://www.facebook.com/index.php");
}


Next, change the line where it says "WebBrowserDocumentCompletedEventHandler(this, &winform::Form1::myDocComplete)" to the name of your namespace. Then we're all done!

Here's a demo project made in VC++ 2008 for those of you that need it. Open winform.sln, compile and run!

Click here to download the demo project. If that doesn't work, go here.

2 comments:

siddharth dungarwal said...

actually i am not able to access your sample project

siddharth dungarwal said...

an also your code is executing but not displaying msg box

Post a Comment

 

C++ Universe - The universe compromised of C++ and C#. Powered By Blogger © 2009.