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 - 名稱/值 Tuple 的內容,串行化為
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 內容標頭。 |