farshad22
farshad22

Reputation: 31

How to POST HTML form programmatically and get response

I have a form that gets username and password and after submitting it shows some information of that account. the form is:

<form id="LoginForm" target="Window81525062.79020184" action="Login.aspx" method="post" name="LoginForm">

      <input id="userID" type="text" maxlength="20" style="WIDTH: 160px; HEIGHT: 24px" onkeypress="CheckEnter()">

      <input id="Password" type="password" maxlength="20" style="WIDTH: 160px; HEIGHT: 24px" onkeypress="CheckEnter()">

      <div id="b2" class="social-media-shareTAK" style="width: 60px;">
          <div class="inner"></div>
          <ul>
              <li>
                  <a title="enter" target="_blank">
                       <img src="/DL/Classes/BUTTON/images/Login.png" alt="" style="width: 38px; height: 38px; margin-top: 4px; cursor: pointer; position: absolute; left: 7px;">
                  </a>
              </li>
          </ul>
       </div>
</form>

now I want to enter about 100 account username and password from a database and store information of these accounts to another database. How should I do this? ( I prefer C# but if there is better way please let me know) thanks :)

Upvotes: 1

Views: 3367

Answers (1)

Asons
Asons

Reputation: 87292

Here is a C# sample (server side code gives better control so I recommend that)

        StringBuilder postData = new StringBuilder();

        postData.Append("USERNAME_FIELD_NAME =" + HttpUtility.UrlEncode("USERNAME") + "&");
        postData.Append("PASSWORD_FIELD_NAME =" + HttpUtility.UrlEncode("PASSWORD"));

        // Now to Send Data.
        StreamWriter writer = null;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(THE_URL);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = postData.ToString().Length;
        try
        {
            writer = new StreamWriter(request.GetRequestStream());
            writer.Write(postData.ToString());

        HttpWebResponse WebResp = (HttpWebResponse)request.GetResponse();                     

        //Now, we read the response (the string), and output it.
        Stream Answer = WebResp.GetResponseStream();
        StreamReader _Answer = new StreamReader(Answer);
        Console.WriteLine(_Answer.ReadToEnd());

        }
        finally
        {
            if (writer != null)
                writer.Close();
        }

Sources/HowTo's:

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

http://forums.asp.net/t/1048041.aspx?How+to+programmatically+POST+data+to+an+aspx+page+

How to simulate HTTP POST programatically in ASP.NET?

Upvotes: 2

Related Questions