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

Discussion in 'Programming' started by Thor, Sep 8, 2009.

Thread Status:
Not open for further replies.
  1. Thor

    Thor Member

    Joined:
    Jun 9, 2004
    Messages:
    504
    Likes Received:
    3
    Trophy Points:
    18
    Location:
    Bangalore
    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. :idea:
    ---------------------------------
     
  2. lucifer_is_back

    lucifer_is_back New Member

    Joined:
    Oct 17, 2008
    Messages:
    124
    Likes Received:
    0
    Trophy Points:
    0
    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
    to

    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: Sep 8, 2009
  3. OP
    OP
    Thor

    Thor Member

    Joined:
    Jun 9, 2004
    Messages:
    504
    Likes Received:
    3
    Trophy Points:
    18
    Location:
    Bangalore
    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!
     
Thread Status:
Not open for further replies.

Share This Page