On This Page

FormUrlEncodedContent Class

System.Net.Http.FormUrlEncodedContent

Represents a collection of key/value pairs that is formatted as application/x-www-form-urlencoded.

Syntax


public class FormUrlEncodedContent : HttpContent
                

Constructors

FormUrlEncodedContent

public FormUrlEncodedContent(IEnumerable<KeyValuePair<string, string>> nameValueCollection)

Initializes a new instance of the FormUrlEncodedContent class.

Parameters

  • nameValueCollection
    An object that implements the IEnumerable<KeyValuePair<string, string>> interface and contains the values that are transmitted in the HTTP content.

FormUrlEncodedContent

public FormUrlEncodedContent(IEnumerable<KeyValuePair<string, string>> nameValueCollection, System.Text.Encoding encoding)

Initializes a new instance of the FormUrlEncodedContent class with a specified encoding.

Parameters

  • nameValueCollection
    An object that implements the IEnumerable<KeyValuePair<string, string>> interface and contains the values that are transmitted in the HTTP content.
  • encoding
    The character encoding for the content.

Properties

This class has no properties.

Methods

CopyToAsync

protected override Task CopyToAsync(Stream stream, TransportContext? transportContext)

Copy the HTTP content to a stream. (Inherited from HttpContent)

Dispose

protected override void Dispose(bool disposing)

Releases the unmanaged resources that are used by the HTTP client handler and optionally releases the managed resources.

ComputeContentHeaders

protected override void ComputeContentHeaders(HttpResponseMessageHeaders headers)

Add the computed content headers to the HTTP response.

ReadAsByteArrayAsync

public Task<byte[]> ReadAsByteArrayAsync()

Reads the HTTP content and returns the content as a byte array.

ReadAsStringAsync

public Task<string> ReadAsStringAsync()

Reads the HTTP content and returns it as a string.

Remarks

The FormUrlEncodedContent class is used to send data in the application/x-www-form-urlencoded format, which is commonly used for submitting HTML form data.

When you create an instance of FormUrlEncodedContent, you typically pass a collection of KeyValuePair<string, string> objects. These represent the key-value pairs that will be encoded and sent in the request body.

The HttpClient class uses FormUrlEncodedContent implicitly when you call PostAsync or PutAsync with a dictionary or collection of key-value pairs.

Example

This example shows how to create and send a POST request with form URL-encoded data.


using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

public class Example
{
    public static async Task SendFormEncodedData()
    {
        using (var client = new HttpClient())
        {
            // Create a dictionary of key-value pairs
            var formData = new Dictionary<string, string>
            {
                { "username", "testuser" },
                { "password", "securepassword123" }
            };

            // Create FormUrlEncodedContent from the dictionary
            var content = new FormUrlEncodedContent(formData);

            // Send the POST request
            var response = await client.PostAsync("https://example.com/api/login", content);

            // Check the response
            response.EnsureSuccessStatusCode(); // Throw an exception if the status code is not successful

            string responseBody = await response.Content.ReadAsStringAsync();
            Console.WriteLine(responseBody);
        }
    }
}