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 - 字节数组表示的内容也用作 StringContent 和 FormUrlEncodedContent的基类。
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 内容标头。 |