Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to submit http form using C#

I have a simple html file such as

<form action="http://www.someurl.com/page.php" method="POST">    <input type="text" name="test"><br/>    <input type="submit" name="submit"> </form> 

Edit: I may not have been clear enough with the question

I want to write C# code which submits this form in the exact same manner that would occur had I pasted the above html into a file, opened it with IE and submitted it with the browser.

like image 501
JC. Avatar asked Aug 13 '09 19:08

JC.


People also ask

How to submit form data in HTML?

The <input type="submit"> defines a button for submitting the form data to a form-handler. The form-handler is typically a file on the server with a script for processing input data. The form-handler is specified in the form's action attribute.

How to send data in GET method?

get( url, [data], [callback], [type] ) method loads data from the server using a GET HTTP request. data − This optional parameter represents key/value pairs that will be sent to the server. callback − This optional parameter represents a function to be executed whenever the data is loaded successfully.

What is form data in HTTP request?

Form data is information provided by the user through interaction with an element in a HTML form, such as a text input box, button, or check box. The information is transmitted as a series of name and value pairs.


2 Answers

Here is a sample script that I recently used in a Gateway POST transaction that receives a GET response. Are you using this in a custom C# form? Whatever your purpose, just replace the String fields (username, password, etc.) with the parameters from your form.

private String readHtmlPage(string url)    {      //setup some variables      String username  = "demo";     String password  = "password";     String firstname = "John";     String lastname  = "Smith";      //setup some variables end        String result = "";       String strPost = "username="+username+"&password="+password+"&firstname="+firstname+"&lastname="+lastname;       StreamWriter myWriter = null;        HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);       objRequest.Method = "POST";       objRequest.ContentLength = strPost.Length;       objRequest.ContentType = "application/x-www-form-urlencoded";        try       {          myWriter = new StreamWriter(objRequest.GetRequestStream());          myWriter.Write(strPost);       }       catch (Exception e)        {          return e.Message;       }       finally {          myWriter.Close();       }        HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();       using (StreamReader sr =           new StreamReader(objResponse.GetResponseStream()) )       {          result = sr.ReadToEnd();           // Close and clean up the StreamReader          sr.Close();       }       return result;    }  
like image 193
shanabus Avatar answered Oct 02 '22 10:10

shanabus


Your HTML file is not going to interact with C# directly, but you can write some C# to behave as if it were the HTML file.

For example: there is a class called System.Net.WebClient with simple methods:

using System.Net; using System.Collections.Specialized;  ... using(WebClient client = new WebClient()) {      NameValueCollection vals = new NameValueCollection();     vals.Add("test", "test string");     client.UploadValues("http://www.someurl.com/page.php", vals); } 

For more documentation and features, refer to the MSDN page.

like image 27
Jeff Meatball Yang Avatar answered Oct 02 '22 08:10

Jeff Meatball Yang