HTML Form Editor in C#

Aspose.HTML for .NET can work with HTML forms as structured document elements. The Aspose.Html.Forms namespace provides APIs for finding a form, accessing input controls, filling values, saving the filled document, submitting data, and inspecting the server response.

To fill and submit an HTML form in C#, load the form page with HTMLDocument, create a FormEditor, set input values through the editor, create a FormSubmitter, call Submit(), and inspect the returned SubmissionResult.

HTML Form Element

The HTMLFormElement represents a section of a document that contains input controls for submitting user information. Using Aspose.HTML for .NET, you can fill HTML forms and send data directly from a C# application.

To make this process more convenient, Aspose.HTML provides the Aspose.Html.Forms namespace. The main class in this namespace is FormEditor. It lets you find an existing form in the document, create a new one from scratch, iterate through form elements, fill controls with data, and submit the form to a remote server.

For testing purposes, the example in this article uses a form provided by the https://httpbin.org service.

How to Use FormEditor in C#

The following C# example shows how to fill and submit an HTML form programmatically.

  1. Create an HTMLDocument from the URL of the page that contains the form.
  2. Use FormEditor.Create(document, index) to create a FormEditor for the selected form. Index 0 refers to the first form in the document.
  3. Access form controls through the editor and set the required input values.
  4. Save the filled HTML form when you need a local copy of the submitted state.
  5. Create a FormSubmitter from the editor.
  6. Call Submit() to send the form data.
  7. Check SubmissionResult.IsSuccess and inspect the response. If the response is HTML, use LoadDocument() for further processing.
 1// How to programmatically fill and submit an HTML form in C#
 2
 3// Initialize an instance of HTML document
 4using (HTMLDocument document = new HTMLDocument(Path.Combine(DataDir, "form.html")))
 5{
 6    // Create an instance of Form Editor
 7    using (FormEditor editor = Aspose.Html.Forms.FormEditor.Create(document, 0))
 8    {
 9        // You can fill the data up using direct access to the input elements, like this:
10        editor["custname"].Value = "John Doe";
11
12        document.Save("out.html");
13
14        // or this:
15        TextAreaElement comments = editor.GetElement<Aspose.Html.Forms.TextAreaElement>("comments");
16        comments.Value = "MORE CHEESE PLEASE!";
17
18        // or even by performing a bulk operation, like this one:
19        editor.Fill(new Dictionary<string, string>()
20        {
21            {"custemail", "john.doe@gmail.com"},
22            {"custtel", "+1202-555-0290"}
23        });
24
25        // Create an instance of form submitter
26        using (FormSubmitter submitter = new Aspose.Html.Forms.FormSubmitter(editor))
27        {
28            // Submit the form data to the remote server
29            // If you need you can specify user-credentials and timeout for the request, etc.
30            SubmissionResult result = submitter.Submit();
31
32            // Check the status of the result object
33            if (result.IsSuccess)
34            {
35                // Check whether the result is in json format
36                if (result.ResponseMessage.Headers.ContentType.MediaType == "application/json")
37                {
38                    // Dump result data to the console
39                    Console.WriteLine(result.Content.ReadAsString());
40                }
41                else
42                {
43                    // Load the result data as an HTML document
44                    using (Document resultDocument = result.LoadDocument())
45                    {
46                        // Inspect HTML document here.
47                    }
48                }
49            }
50        }
51    }
52}

Common HTML Form Submission Issues

IssueCauseFix
The wrong form is filledThe page contains several <form> elements and the wrong index was used.Confirm the form order in the source document or locate the intended form before creating the editor.
A value is not submittedThe input control name does not match the form field expected by the server.Inspect the form markup and set values for the correct named controls.
Submission failsThe action URL is unavailable, blocked, or requires authentication, cookies, or additional headers.Check the target URL and configure networking or message handlers when the request needs custom behavior.
Response handling is incorrectThe response is not HTML or the server returned JSON, plain text, or an error page.Check SubmissionResult.IsSuccess and inspect the response type before loading it as an HTMLDocument.

FAQ

Can Aspose.HTML for .NET fill existing HTML forms?

Yes. Load the page with HTMLDocument, create a FormEditor, and set values on the form controls before saving or submitting the form.

Can FormEditor submit a form to a remote server?

Yes. Use FormSubmitter with the edited form and call Submit(). The result can then be checked through SubmissionResult.

Can I save a filled form without submitting it?

Yes. After setting form values, save the HTMLDocument if you need a local HTML file that contains the filled state.

Related Articles