Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 08-09-2009, 11:27 AM   #1 (permalink)
In The Zone
 
Thor's Avatar
 
Join Date: Jun 2004
Location: Kolkata
Posts: 384
Arrow C#/.NET WinForms Application to login to a forum using HttpWebRequest and POST


Hi,

I am working on building a C# Winforms application which will allow me to login to a forum or site and perform certain tasks. I am stuck at the very 1st step. I cannot login to the forum. Here is what I have tried so far,
the Login button has the following code :-

Code:
try
			{
				Uri uri = new Uri("http://www.thinkdigit.com/forum/login.php");
				string data = "vb_login_username=userid&vb_login_password=password";
				HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
				request.Method = WebRequestMethods.Http.Post;
				request.ContentLength = data.Length;
				request.ContentType = "application/x-www-form-urlencoded";
				StreamWriter writer = new StreamWriter(request.GetRequestStream());
				writer.Write(data);
				writer.Close();
				HttpWebResponse response = (HttpWebResponse)request.GetResponse();
				StreamReader reader = new StreamReader(response.GetResponseStream());
				string tmp = reader.ReadToEnd();
				response.Close();
				richTextBox1.AppendText(tmp);			   
			}
			catch (Exception ex)
			{
				textBox3.Text = ex.ToString();
			}
Thats what I scrounged up after google busting and visiting so many forum posts. Unfortunately this does not seem to log me in the forum. I am checking the HTML generated content in t my richtextbox control and there's the login fields there still. Which means it didn't login.

I checked the login page.

and these are the contents there

Code:
<form name="loginfrm" id="loginfrm" action="login.php?do=login" method="post" onsubmit="md5hash_2(vb_login_password, vb_login_md5password, vb_login_md5password_utf, 0);">

<input type="text" class="bginput" style="font-size: 11px" name="vb_login_username" id="navbar_username" size="10" accesskey="u" tabindex="101" value="User Name" onblur="myUnTip();" onmouseout="myUnTip();" onfocus="if (this.value == 'User Name') this.value = '';myTip();"   />

<input type="password" class="bginput" style="font-size: 11px" name="vb_login_password" id="navbar_password" size="10" tabindex="102" />

<input type="submit" class="button" value="Log in" tabindex="104" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s" />
those are the relevant parts IMHO

so I am filling up the username field, filling up the password, posting to the login.php page with the data ...what am i mising out ?

Also I have another query. Is there a way to load the response , as in HttpGetResponse in a webbroswer control ? If I am login in to the forum, then it would be nice to have the logged in page loaded in my webbrowser control!!

Any Help Would be appreciated.
---------------------------------
__________________
Beware of my Wrath Mortals, Thou hast no choice.
Thor is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 08-09-2009, 10:21 PM   #2 (permalink)
Broken In
 
Join Date: Oct 2008
Posts: 133
Default Re: C#/.NET WinForms Application to login to a forum using HttpWebRequest and POST

Code:
try
            {
                Uri uri = new Uri("http://www.thinkdigit.com/forum/login.php");
                string data = "vb_login_username="+userid+"&vb_login_password="+password;
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
                request.Method = WebRequestMethods.Http.Post;
                request.ContentLength = data.Length;
                request.ContentType = "application/x-www-form-urlencoded";
                StreamWriter writer = new StreamWriter(request.GetRequestStream());
                writer.Write(data);
                writer.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string tmp = reader.ReadToEnd();
                response.Close();
                richTextBox1.AppendText(tmp);
            }
            catch (Exception ex)
            {
                textBox3.Text = ex.ToString();
            }
changed
Quote:
string data = "vb_login_username=userid&vb_login_password=passwo rd";
to

Quote:
string data = "vb_login_username="+userid+"&vb_login_password="+password;
where userid n password r textbox
-----------------------------------------
Posted again:
-----------------------------------------
there is one more thing
u have to make md5 hash of the the password
check the Html code u get

Last edited by lucifer_is_back; 08-09-2009 at 10:26 PM. Reason: Automerged Doublepost
lucifer_is_back is offline  
Old 09-09-2009, 12:04 AM   #3 (permalink)
In The Zone
 
Thor's Avatar
 
Join Date: Jun 2004
Location: Kolkata
Posts: 384
Default Re: C#/.NET WinForms Application to login to a forum using HttpWebRequest and POST

Hi Lucifer,
thanks for dropping by.

Yes, by userid and password I meant the actual hardcoded values and not the variables. For testing purposes. In actual scenario i wud have taken them from textbox input.

I also tried with md5 hashing the password. I will try once again and post with the clean up code of what I tried.

What about my 2nd question ? Is there a way to get the HttpGetResponse loaded in Webbrowser control ?

Thanks again in advance!
__________________
Beware of my Wrath Mortals, Thou hast no choice.
Thor is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Charan
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 03:11 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2