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



## Thor (Sep 8, 2009)

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("*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.   
---------------------------------


----------



## lucifer_is_back (Sep 8, 2009)

```
try
            {
                Uri uri = new Uri("*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


> string data = "vb_login_username=userid&vb_login_password=password";


to 



> 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


----------



## Thor (Sep 9, 2009)

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!


----------

