C#/.NET code for POST HTML Form Example
This C#/.NET code snippet was generated automatically for the POST HTML Form example.<< Back to the POST HTML Form example
What is HTTP POST?
HTTP POST is one of the nine standard methods of the Hypertext Transfer Protocol. The POST method is used to post data to the server, upload files and images, and submit HTML forms. The HTTP POST method differs from HTTP GET and HEAD requests in that POST requests can change the server's state.
What is HTML Form?
A form is a section of an HTML document that contains controls such as text and password input fields, radio buttons, checkboxes, and a Submit button, enclosed in an HTML <form> tag. HTML forms collect user input and send it to a server for processing. Examples of HTML forms are login and image upload forms.
Submitting HTML Forms over HTTP with C#/.NET
In HTTP, there are two ways to submit HTML forms to the server using the application/x-www-form-urlencoded and multipart/form-data content types. HTML forms can be submitted from browsers and from C#/.NET code.
- Submitting HTML forms using the application/x-www-form-urlencoded media type.
The application/x-www-form-urlencoded media type is mainly used for submitting short HTML forms as key-value pairs, for example, when authorizing a user using login/password forms.
- Submitting HTML forms using the multipart/form-data media type.
The multipart/form-data media type is used by browsers when uploading binary data to the server (for example, when uploading multiple images). In this case, the form data is formatted as a sequence of parts separated by MIME boundaries.
Submitting HTML Forms in JSON Format
HTML does not have native methods for converting forms to JSON objects. If you want to convert form data to JSON, you need to use JavaScript to:
- Collect form data from a DOM object into a JavaScript object. If you are using jQuery, you can convert your HTML form into a JavaScript object with a single line using the $("#form").serializeArray() jQuery method.
- Convert the JavaScript object to JSON string using JSON.stringify() method.
- Send the form data to the server using the XMLHttpRequest or fetch methods.
Submitting HTML Forms using the HTTP GET Method
In some cases, you can submit form data using the HTTP GET method. The form data is passed to the server as a series of name/value URL parameters, and you are limited to the maximum length of the URL. For browsers, the limit is around 2000 characters (browser dependent) for the C#/.NET code, there is no such limit, but your server may have its limitations. For example, the maximum length of an Apache URL is 8177 characters. Submitting HTML forms using the HTTP GET method is less secure because you pass URL parameters data.