HttpContent Class
System.Net.Http
Represents an HTTP protocol payload body.
Remarks
The HttpContent class is an abstract base class that represents the HTTP protocol payload body.
It is used to represent the content of an HTTP request or response. Concrete implementations of HttpContent are provided for various content types, such as:
StringContent: For plain text or JSON content.FormUrlEncodedContent: For form URL-encoded data.StreamContent: For streaming content from aStream.ByteArrayContent: For content represented as a byte array.MultipartContent: For multipart MIME types, such as form data.
HttpContent objects can be used to send data in an HTTP request or receive data in an HTTP response. They are typically created and managed by HttpClient, but can also be created manually.
It's important to dispose of HttpContent objects when they are no longer needed to release any unmanaged resources.
Constructors
| Signature | Description |
|---|---|
HttpContent() |
Initializes a new instance of the HttpContent class. |
Methods
protected virtual void Dispose(bool disposing)
Releases the unmanaged resources that are used by the
HttpContent, and optionally releases the managed resources.public void Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
protected virtual Task SerializeToStreamAsync(Stream stream, TransportContext? context)
Copies the HTTP content to a stream.
| Name | Description |
|---|---|
stream | The stream to write to. |
context | Information about the transport. |
Returns:
Task<int> A task object that indicates when the operation has completed. The value of the task object contains the number of bytes written to the stream.public Task LoadIntoBufferAsync()
Copies the HTTP content to a memory buffer.
Returns:
Task A task object that indicates when the operation has completed.public Task ReadAsStringAsync()
Reads the HTTP content as a string.
Returns:
Task<string> A task object that indicates when the operation has completed. The value of the task object contains the content as a string.public Task ReadAsByteArrayAsync()
Reads the HTTP content as a byte array.
Returns:
Task<byte[]> A task object that indicates when the operation has completed. The value of the task object contains the content as a byte array.public Task ReadAsStreamAsync()
Reads the HTTP content as a stream.
Returns:
Task<Stream> A task object that indicates when the operation has completed. The value of the task object contains the content as a stream.protected virtual bool TryComputeLength(out long length)
Attempts to compute the length of the HTTP content.
| Name | Description |
|---|---|
length | When this method returns, contains the length of the HTTP content. |
Returns:
bool true if the length could be computed; otherwise, false.Properties
public Task ContentReadStream { get; }
Gets the content read from the HTTP content.
public HttpHeaders Headers { get; }
Gets the HTTP headers for the content.
Example
This example shows how to create a StringContent object and send it in an HTTP POST request.
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class Example
{
public static async Task PostDataAsync()
{
using (var client = new HttpClient())
{
var content = new StringContent("This is the request body.", System.Text.Encoding.UTF8, "text/plain");
var response = await client.PostAsync("https://example.com/api/resource", content);
response.EnsureSuccessStatusCode(); // Throws an exception if the status code is not a success code.
Console.WriteLine($"Response status code: {response.StatusCode}");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Response body: {responseBody}");
}
}
}