HTML 表单编辑器 – C#

HTML 表格元素

HTML Form Element 表示包含用于提交用户信息的输入控件的部分。使用我们的库,您可以轻松完成 HTML 表单,并直接从 C# 应用程序中发送数据。为了使这一过程更加轻松,我们设计了 Aspose.Html.Forms 命名空间。该命名空间的主要元素是 FormEditor。它允许您在文档中查找现有表单,从头开始创建一个新表单,遍历元素,填充数据,并将信息发送到远程服务器。

在本文中,您将学习如何使用 FormEditor 对象来操作文档中的 HTML 表单、输入控件和提交信息。为了测试目的,我们使用 https://httpbin.org 服务设计的表单。

如何使用表单编辑器

让我们来看看演示如何使用 Aspose.Html.Forms 命名空间以编程方式填写和提交 HTML 表单的 C# 代码片段。您需要遵循以下步骤:

  1. 使用 HTMLDocument(url) 构造函数创建 HTMLDocument 类的新实例。指定要填写和提交的 HTML 表单的 URL。
  2. 使用 Create(document, index) 方法创建一个 FormEditor 类实例,并在其中传递 HTMLDocument 实例和要编辑的表单的索引(值 0 指 HTML 文档中的第一个表单)。
  3. 通过直接访问输入元素来填写数据,并将填写好的 HTML 表单保存到文件中。表单字段可使用 “编辑器 “对象访问和修改。
  4. 使用 FormSubmitter(editor) 构造函数创建 FormSubmitter 类的实例。
  5. 调用 FormSubmitter 类的 Submit() 方法提交表单。
  6. 使用 IsSuccess 属性检查表单提交结果的状态。如果表单提交成功,则检查服务器的响应:
    • 如果响应是 JSON 格式,则将 JSON 内容打印到控制台;
    • 否则,使用 LoadDocument() 方法将响应加载到另一个 HTMLDocument 实例中,以供进一步检查。
 1// How to 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}

Aspose.HTML 提供 HTML 网络应用程序,它是免费转换器、合并器、搜索引擎优化工具、HTML 代码生成器、URL 工具等的在线集合。这些应用程序可在任何装有网络浏览器的操作系统上运行,无需安装任何其他软件。无论你身在何处,都能轻松转换、合并、编码、生成 HTML 代码,从网络中提取数据,或从搜索引擎优化的角度分析网页。使用我们的 HTML 网络应用程序集来处理您的日常事务,让您的工作流程天衣无缝!

文本 “HTML 网络应用程序”

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.