HttpContent 类

定义

表示 HTTP 实体正文和内容标头的基类。

public ref class HttpContent abstract : IDisposable
public abstract class HttpContent : IDisposable
type HttpContent = class
    interface IDisposable
Public MustInherit Class HttpContent
Implements IDisposable
继承
HttpContent
派生
实现

示例

以下示例演示 HttpContent的自定义实现。 尽管某些方法被定义为 virtual 而不是 abstract,但在实现中仍应重写以实现最佳行为。

public class MyContent : HttpContent
{
    private readonly string _data;
    public MyContent(string data)
    {
        _data = data;
    }

    // Minimal implementation needed for an HTTP request content,
    // i.e. a content that will be sent via HttpClient, contains the 2 following methods.
    protected override bool TryComputeLength(out long length)
    {
        // This content doesn't support pre-computed length and
        // the request will NOT contain Content-Length header.
        length = 0;
        return false;
    }

    // SerializeToStream* methods are internally used by CopyTo* methods
    // which in turn are used to copy the content to the NetworkStream.
    protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context)
        => stream.WriteAsync(Encoding.UTF8.GetBytes(_data)).AsTask();

    // Override SerializeToStreamAsync overload with CancellationToken
    // if the content serialization supports cancellation, otherwise the token will be dropped.
    protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken)
        => stream.WriteAsync(Encoding.UTF8.GetBytes(_data), cancellationToken).AsTask();

    // In rare cases when synchronous support is needed, e.g. synchronous CopyTo used by HttpClient.Send,
    // implement synchronous version of SerializeToStream.
    protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellationToken)
        => stream.Write(Encoding.UTF8.GetBytes(_data));

    // CreateContentReadStream* methods, if implemented, will be used by ReadAsStream* methods
    // to get the underlying stream and avoid buffering.
    // These methods will not be used by HttpClient on a custom content.
    // They are for content receiving and HttpClient uses its own internal implementation for an HTTP response content.
    protected override Task<Stream> CreateContentReadStreamAsync()
        => Task.FromResult<Stream>(new MemoryStream(Encoding.UTF8.GetBytes(_data)));

    // Override CreateContentReadStreamAsync overload with CancellationToken
    // if the content serialization supports cancellation, otherwise the token will be dropped.
    protected override Task<Stream> CreateContentReadStreamAsync(CancellationToken cancellationToken)
        => Task.FromResult<Stream>(new MemoryStream(Encoding.UTF8.GetBytes(_data))).WaitAsync(cancellationToken);

    // In rare cases when synchronous support is needed, e.g. synchronous ReadAsStream,
    // implement synchronous version of CreateContentRead.
    protected override Stream CreateContentReadStream(CancellationToken cancellationToken)
        => new MemoryStream(Encoding.UTF8.GetBytes(_data));
}

注解

可以使用各种 HTTP 内容。 其中包括以下内容。

  1. ByteArrayContent - 字节数组表示的内容也用作 StringContentFormUrlEncodedContent的基类。

  2. StringContent - 基于字符串的内容,默认情况下,使用 UTF-8 编码序列化为 text/plainContent-Type

  3. FormUrlEncodedContent - 名称/值元组的内容,序列化为 application/x-www-form-urlencodedContent-Type

  4. MultipartContent - 可将多个不同 HttpContent 对象序列化为 multipart/*Content-Type的内容。

  5. JsonContent - 默认情况下,使用 UTF-8 编码将对象序列化为 application/jsonContent-Type 的内容。

HTTP 内容类可由用户派生以提供自定义内容序列化逻辑。

构造函数

HttpContent()

初始化 HttpContent 类的新实例。

属性

Headers

获取 RFC 2616 中定义的 HTTP 内容标头。

方法

CopyTo(Stream, TransportContext, CancellationToken)

将 HTTP 内容序列化为字节流,并将其复制到 stream

CopyToAsync(Stream)

将 HTTP 内容序列化为字节流,并将其复制到作为 stream 参数提供的流对象。

CopyToAsync(Stream, CancellationToken)

将 HTTP 内容序列化为字节流,并将其复制到作为 stream 参数提供的流对象。

CopyToAsync(Stream, TransportContext)

将 HTTP 内容序列化为字节流,并将其复制到作为 stream 参数提供的流对象。

CopyToAsync(Stream, TransportContext, CancellationToken)

将 HTTP 内容序列化为字节流,并将其复制到作为 stream 参数提供的流对象。

CreateContentReadStream(CancellationToken)

将 HTTP 内容序列化为内存流。

CreateContentReadStreamAsync()

将 HTTP 内容序列化为异步操作的内存流。

CreateContentReadStreamAsync(CancellationToken)

将 HTTP 内容序列化为异步操作的内存流。

Dispose()

释放非托管资源并释放 HttpContent使用的托管资源。

Dispose(Boolean)

释放 HttpContent 使用的非托管资源,并选择性地释放托管资源。

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
LoadIntoBufferAsync()

将 HTTP 内容序列化为异步操作的内存缓冲区。

LoadIntoBufferAsync(Int64)

将 HTTP 内容序列化为异步操作的内存缓冲区。

MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ReadAsByteArrayAsync()

将 HTTP 内容序列化为异步操作的字节数组。

ReadAsByteArrayAsync(CancellationToken)

将 HTTP 内容序列化为异步操作的字节数组。

ReadAsStream()

序列化 HTTP 内容并返回表示内容的流。

ReadAsStream(CancellationToken)

序列化 HTTP 内容并返回表示内容的流。

ReadAsStreamAsync()

序列化 HTTP 内容并返回将内容表示为异步操作的流。

ReadAsStreamAsync(CancellationToken)

序列化 HTTP 内容并返回将内容表示为异步操作的流。

ReadAsStringAsync()

将 HTTP 内容序列化为异步操作的字符串。

ReadAsStringAsync(CancellationToken)

将 HTTP 内容序列化为异步操作的字符串。

SerializeToStream(Stream, TransportContext, CancellationToken)

在派生类中重写时,将 HTTP 内容序列化为流。 否则,将引发 NotSupportedException

SerializeToStreamAsync(Stream, TransportContext)

将 HTTP 内容序列化为异步操作流。

SerializeToStreamAsync(Stream, TransportContext, CancellationToken)

将 HTTP 内容序列化为异步操作流。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)
TryComputeLength(Int64)

确定 HTTP 内容是否具有有效的长度(以字节为单位)。

扩展方法

ReadFromJsonAsAsyncEnumerable<TValue>(HttpContent, JsonSerializerOptions, CancellationToken)

读取 HTTP 内容并返回在异步可枚举操作中将内容反序列化为 JSON 的结果的值。

ReadFromJsonAsAsyncEnumerable<TValue>(HttpContent, JsonTypeInfo<TValue>, CancellationToken)

读取 HTTP 内容并返回在异步可枚举操作中将内容反序列化为 JSON 的结果的值。

ReadFromJsonAsAsyncEnumerable<TValue>(HttpContent, CancellationToken)

读取 HTTP 内容并返回在异步可枚举操作中将内容反序列化为 JSON 的结果的值。

ReadFromJsonAsync(HttpContent, Type, JsonSerializerOptions, CancellationToken)

读取 HTTP 内容并返回在异步操作中将内容反序列化为 JSON 产生的值。

ReadFromJsonAsync(HttpContent, Type, JsonSerializerContext, CancellationToken)

读取 HTTP 内容并返回在异步操作中将内容反序列化为 JSON 产生的值。

ReadFromJsonAsync(HttpContent, Type, CancellationToken)

读取 HTTP 内容并返回在异步操作中将内容反序列化为 JSON 产生的值。

ReadFromJsonAsync<T>(HttpContent, JsonSerializerOptions, CancellationToken)

读取 HTTP 内容并返回在异步操作中将内容反序列化为 JSON 产生的值。

ReadFromJsonAsync<T>(HttpContent, JsonTypeInfo<T>, CancellationToken)

读取 HTTP 内容并返回在异步操作中将内容反序列化为 JSON 产生的值。

ReadFromJsonAsync<T>(HttpContent, CancellationToken)

读取 HTTP 内容并返回在异步操作中将内容反序列化为 JSON 产生的值。

适用于