共用方式為


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 - 位元組陣列所代表的內容,也會做為 StringContentFormUrlEncodedContent的基類。

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

  3. FormUrlEncodedContent - 名稱/值 Tuple 的內容,串行化為 application/x-www-form-urlencodedContent-Type

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

  5. JsonContent - 預設會將物件串行化為具有 UTF-8 編碼方式 application/jsonContent-Type 的內容。

HTTP 內容類別可由使用者衍生,以提供自定義內容串行化邏輯。

建構函式

HttpContent()

初始化 HttpContent 類別的新實例。

屬性

Headers

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

方法

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 資源,並選擇性地處置受控資源。

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 所產生的值。

適用於