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- 預設會將物件序列化為
application/json
Content-Type
編碼UTF-8
的內容。
HTTP 內容類別別可由使用者衍生,以提供自訂內容序列化邏輯。
建構函式
HttpContent() |
初始化 HttpContent 類別的新執行個體。 |
屬性
Headers |
取得 HTTP 內容標頭,如 RFC 2616 中所定義。 |