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- 由位元組陣列表示的內容,也可以做為 和 FormUrlEncodedContentStringContent 基類。

  2. StringContent- 字串型內容,預設會以 UTF-8 編碼方式序列化為 。 text/plainContent-Type

  3. FormUrlEncodedContent- 名稱/值元組的內容,序列化為 application/x-www-form-urlencodedContent-Type

  4. MultipartContent- 可將多個不同 HttpContent 物件序列化為 multipart/*Content-Type 的內容。

  5. JsonContent- 預設會將物件序列化為 application/jsonContent-Type 編碼 UTF-8 的內容。

HTTP 內容類別別可由使用者衍生,以提供自訂內容序列化邏輯。

建構函式

HttpContent()

初始化 HttpContent 類別的新執行個體。

屬性

Headers

取得 HTTP 內容標頭,如 RFC 2616 中所定義。

方法

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()

釋放 Unmanaged 資源,並處置 HttpContent 所使用的 Managed 資源。

Dispose(Boolean)

釋放 HttpContent 所使用的 Unmanaged 資源,並選擇性處置 Managed 資源。

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 還原序列化後所產生的值。

適用於