Namespace: System.Net.Http
Assembly: System.Net.Http.dll

HttpContent Class

Represents an HTTP content entity. This is an abstract class.

Note: HttpContent is the base class for objects that represent the HTTP entity body and the Content-Type header.
Developer guidance: Use this class to serialize and deserialize HTTP content when performing HTTP operations.

Inheritance

System.Object
System.Net.Http.HttpContent

Syntax

public abstract class HttpContent

Remarks

The HttpContent class is used to represent the HTTP message body and the Content-Type header. It provides methods for serializing the content to an HTTP message, deserializing content from an HTTP message, and for accessing the content headers. Common derived classes include:

When you are done with an HttpContent object, it is important to dispose of it to release any unmanaged resources it may hold. This can be done using a using statement.

Constructors

The HttpContent class has no public constructors because it is an abstract class.

Methods

Dispose()

Releases the unmanaged resources that are used by the abstract base class and, optionally, releases the managed resources.

protected virtual void Dispose(bool disposing)

ReadAsByteArrayAsync()

Reads the HTTP content as an asynchronous operation to a byte array.

public Task<byte[]> ReadAsByteArrayAsync()

Returns:Task<byte[]>. A task object that indicates the completion of the operation.

ReadAsStreamAsync()

Reads the HTTP content as an asynchronous operation to a stream.

public Task<Stream> ReadAsStreamAsync()

Returns:Task<Stream>. A task object that indicates the completion of the operation.

ReadAsStringAsync()

Reads the HTTP content as an asynchronous operation to a string.

public Task<string> ReadAsStringAsync()

Returns:Task<string>. A task object that indicates the completion of the operation.

TryComputeLength()

Tries to compute the length of the HTTP content.

protected abstract bool TryComputeLength(out long length)

Parameters:

Returns:bool. true if the length can be computed; otherwise, false.

Properties

Headers

Gets an enumeration of HTTP content headers as defined in the HTTP specification.

public abstract HttpContentHeaders Headers { get; }

Returns:HttpContentHeaders. An instance of HttpContentHeaders that contains the HTTP content headers.

Derived Classes

Class Description
ByteArrayContent Represents an HTTP entity body as a byte array.
MultipartContent Represents a collection of HTTP content parts.
MultipartFormDataContent Represents a multipart/form-data HTTP entity body.
StreamContent Represents an HTTP entity body as a stream.
StringContent Represents an HTTP entity body as a string.

See Also