C#/.NET WinForms Application to login to a forum using HttpWebRequest and POST

Sep 8 2009 1:05 AM
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 :-

            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

<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.

thanks a bunch folks for your time!!

Answers (1)