HttpContent 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示 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 内容。 这些组件包括:
ByteArrayContent- 由字节数组表示的内容,也用作 和 FormUrlEncodedContent的StringContent基类。
StringContent- 基于字符串的内容,默认使用
UTF-8
编码序列化为 。text/plain
Content-Type
FormUrlEncodedContent - 名称/值元组序列化为
application/x-www-form-urlencoded
Content-Type
的内容。MultipartContent - 可将多个不同 HttpContent 对象序列化为
multipart/*
Content-Type
的内容。JsonContent- 默认使用
UTF-8
编码将对象序列化为application/json
Content-Type
的内容。
用户可派生 HTTP 内容类以提供自定义内容序列化逻辑。
构造函数
HttpContent() |
初始化 HttpContent 类的新实例。 |
属性
Headers |
获取 RFC 2616 中定义的 HTTP 内容标头。 |
方法
扩展方法
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈