2.3Form Form Submission (URL Encoding)

Created on Aug 17, 2026~9 min read

In web applications, the most common way to save user-defined data is through Form submission. A Form can carry not only text data but also binary data, such as files.

cs
var content = await httpRemoteService.PostAsAsync<YourRemoteFormResult>("https://localhost:7044/HttpRemote/AddForm?id=1",    builder => builder.SetMultipartContent(multipart => multipart   // Set multipart form content        .AddJson(new { id = 1, name = "furion" })   // Set JSON data        .AddFileAsStream(@"C:\Workspaces\httptest.jpg", "file")));  // Set a file (supports stream, byte array, remote URL, and Base64 string forms)

The SetMultipartContent method is specifically used to set the request content type to multipart/form-data form data, and it provides a rich set of extension options, including:

  • Boundary or SetBoundary(boundary): Sets the multipart form content boundary.
  • AddJson(rawJson): Adds JSON content.
  • AddFormItem(value, name): Adds a single form item content.
  • AddHtml(htmlString, name): Adds HTML content.
  • AddXml(xmlString, name): Adds XML content.
  • AddText(text, name): Adds text content.
  • AddObject(rawObject, name): Adds object content.
  • AddFileFromRemote(url, name): Adds internet file content.
  • AddFileFromBase64String(base64String, name, fileName): Adds Base64 string file content.
  • AddFileAsStream(path, name): Adds a local file as stream content.
  • AddFileWithProgressAsStream(path, channel, name): Adds a local file as stream content (with file transfer progress).
  • AddFileAsByteArray(path, name): Adds a local file as byte array content.
  • AddFile(multipartFile, name): Adds MultipartFile file content.
  • AddFile(fileInfo, name): Adds FileInfo file content.
  • AddFile(IFormFile): Adds IFormFile file content.
  • AddFiles(IFormFileCollection): Adds IFormFileCollection file content.
  • AddFile(IBrowserFile): Adds IBrowserFile file content.
  • AddFiles(IEnumerable<IBrowserFile>): Adds multiple IBrowserFile file content.
  • AddStream(stream, name): Adds binary stream content.
  • AddByteArray(byteArray, name): Adds binary byte array content.
  • Add(httpContent): Adds HttpContent content.

These are only the commonly used methods; SetMultipartContent provides even more flexibility.

In addition to the approaches above, the following methods are also supported:

cs
// Using the builder patternvar content = await httpRemoteService.SendAsAsync<YourRemoteFormResult>(HttpRequestBuilder.Post("https://localhost:7044/HttpRemote/AddForm?id=1")    .SetMultipartContent(multipart => multipart   // Set multipart form content        .AddJson(new { id = 1, name = "furion" })   // Set JSON data        .AddFileAsStream(@"C:\Workspaces\httptest.jpg", "file")));  // Set a file (supports stream, byte array, remote URL, and Base64 string forms// For more detailed usage, see Section 2.1

The following are some common examples of Form submission:

cs
var content = await httpRemoteService.PostAsAsync<YourRemoteFormResult>("https://localhost:7044/HttpRemote/AddForm?id=1",    builder => builder.SetMultipartContent(multipart => multipart   // Set multipart form content        .AddJson(new { id = 1, name = "furion" })   // Set JSON data        .AddFormItem("age", "Age")  // Supports setting a single value        .AddFileAsStream(@"C:\Workspaces\httptest.jpg", "file") // Set a single file (corresponds to the form's File field)        // Supports internet file URLs        .AddFileFromRemote("https://furion.net/img/furionlogo.png", "files")    // Set multiple files (corresponds to the form's Files field)        // Supports reading a local file as a byte array        .AddFileAsByteArray(@"C:\Workspaces\httptest.jpg", "files"));  // Set multiple files (corresponds to the form's Files field)        // Add a MultipartFile file        .AddFile(MultipartFile.CreateFromPath(@"C:\Workspaces\httptest.jpg")));
  • URL-Encoded Form

In addition to the multipart multipart/form-data form request, another common request type is application/x-www-form-urlencoded, which sends data in URL-encoded form. This type of form is characterized by all special characters being URL-encoded, and it is typically used for simple form submission scenarios that do not require uploading binary data such as files.

The following example shows how to build form data that conforms to the application/x-www-form-urlencoded submission type.

cs
var content = await httpRemoteService.PostAsAsync<YourRemoteModel>("https://localhost:7044/HttpRemote/AddURLForm",    builder => builder        .SetFormUrlEncodedContent(new { id = 1, name = "furion" })); // Set application/x-www-form-urlencoded request content// Supports URL-encoded string formatvar content = await httpRemoteService.PostAsAsync<YourRemoteModel>("https://localhost:7044/HttpRemote/AddURLForm",    builder => builder        .SetFormUrlEncodedContent("id=1&name=furion", useStringContent: true);