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自訂實作。 某些方法,儘管定義為 而 virtualabstract,仍應在實作中覆寫以達到最佳行為。

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-TypeUTF-8的內容。

使用者可以推導出 HTTP 內容類別,以提供自訂的內容序列化邏輯。

建構函式

名稱 Description
HttpContent()

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

屬性

名稱 Description
Headers

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

方法

名稱 Description
CopyToAsync(Stream, TransportContext)

將 HTTP 內容序列化成一串位元組,並複製到作為 stream 參數的串流物件。

CopyToAsync(Stream)

將 HTTP 內容序列化成一串位元組,並複製到作為 stream 參數的串流物件。

CreateContentReadStreamAsync()

將 HTTP 內容序列化為記憶體串流,作為非同步操作。

Dispose()

釋放未管理的資源並處理由 HttpContent.

Dispose(Boolean)

釋放未管理的資源, HttpContent 並可選擇性地處置這些受管理資源。

Equals(Object)

判斷指定的 物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
LoadIntoBufferAsync()

將 HTTP 內容序列化至記憶體緩衝區,作為非同步操作。

LoadIntoBufferAsync(Int64)

將 HTTP 內容序列化至記憶體緩衝區,作為非同步操作。

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ReadAsByteArrayAsync()

將 HTTP 內容序列化成位元組陣列,作為非同步操作。

ReadAsStreamAsync()

串行化 HTTP 內容,並傳回以異步操作表示內容的數據流。

ReadAsStringAsync()

將 HTTP 內容序列化為字串,作為非同步操作。

SerializeToStreamAsync(Stream, TransportContext)

將 HTTP 內容序列化為串流,作為非同步操作。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
TryComputeLength(Int64)

判斷 HTTP 內容是否有有效長度(位元組)。

適用於