System.Net.Http Namespace

System.Net.Http命名空間提供現代 HTTP 應用程式的程式設計介面。 The System.Net.Http namespace provides a programming interface for modern HTTP applications.



提供以位元組陣列為基礎的 HTTP 內容。Provides HTTP content based on a byte array.


HTTP 處理常式的類型,這些處理常式會將 HTTP 回應訊息的處理委派給另一個處理常式,也稱為內部處理常式。A type for HTTP handlers that delegate the processing of HTTP response messages to another handler, called the inner handler.


使用 application/x-www-form-urlencoded MIME 類型編碼之名稱/值 Tuple 的容器。A container for name/value tuples encoded using application/x-www-form-urlencoded MIME type.


提供基底類別,用於傳送 HTTP 要求,以及從 URI 所識別的資源接收 HTTP 回應。Provides a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI.


IHttpClientFactory 的擴充方法。Extensions methods for IHttpClientFactory.


.NET Framework 和 .NET Core 2.0 及更舊版本中 HttpClient 使用的預設訊息處理常式。The default message handler used by HttpClient in .NET Framework and .NET Core 2.0 and earlier.


代表 HTTP 實體內容和內容標頭的基底類別。A base class representing an HTTP entity body and content headers.


HTTP 訊息處理常式的基底類型。A base type for HTTP message handlers.


IHttpMessageHandlerFactory 的擴充方法。Extensions methods for IHttpMessageHandlerFactory.


特定類別,允許應用程式呼叫 HTTP 處理常式鏈結的 SendAsync(HttpRequestMessage, CancellationToken) 方法。A specialty class that allows applications to call the SendAsync(HttpRequestMessage, CancellationToken) method on an HTTP handler chain.


協助程式類別,用於擷取和比較標準 HTTP 方法,以及建立新的 HTTP 方法。A helper class for retrieving and comparing standard HTTP methods and for creating new HTTP methods.


HttpClientHttpMessageHandler 類別所擲回之例外狀況的基底類別。A base class for exceptions thrown by the HttpClient and HttpMessageHandler classes.


表示 HTTP 要求訊息。Represents a HTTP request message.


表示包含狀態碼及資料的 HTTP 回應訊息。Represents a HTTP response message including the status code and data.


處理常式的基底類型,這些處理常式僅對要求和 (或) 回應訊息執行一些小型處理。A base type for handlers which only do some small processing of request and/or response messages.


提供使用 multipart/* 內容類型規格進行序列化之 HttpContent 物件的集合。Provides a collection of HttpContent objects that get serialized using the multipart/* content type specification.


提供使用 multipart/form-data MIME 類型編碼內容的容器。Provides a container for content encoded using multipart/form-data MIME type.


在使用 Xamarin stack (iOS、macOS、watchOS、tvOS) 的 Apple 平台上,由  HttpClient  使用的預設訊息處理常式The default message handler used by HttpClient on Apple platforms using the Xamarin stack (iOS, macOS, watchOS, tvOS)


Provides HTTP content based on a System.ReadOnlyMemory<System.Byte>.


提供 .NET Core 2.1 及更新版本中 HttpClient 使用的預設訊息處理常式。Provides the default message handler used by HttpClient in .NET Core 2.1 and later.


提供以資料流為基礎的 HTTP 內容。Provides HTTP content based on a stream.


提供以字串為基礎的 HTTP 內容。Provides HTTP content based on a string.


提供 Windows 市集應用程式或其他環境中所沒有的桌面特定功能。Provides desktop-specific features not available to Windows Store apps or other environments.


WinHttpHandler 是以 Windows 的 WinHTTP 介面為基礎的特殊訊息處理常式,預定在伺服器環境中使用。WinHttpHandler is a specialty message handler based on the WinHTTP interface of Windows and is intended for use in server environments. 透過將這個類別安裝為 NuGet 封裝,其也可用於桌面應用程式。This class is also available for use in Desktop apps by installing it as a NuGet package. 如需安裝這個類別以用於桌面應用程式的詳細資訊,請參閱 System.Net.Http.WinHttpHandlerFor more information about installing this class for use in Desktop apps, see System.Net.Http.WinHttpHandler.



元件的 Factory 抽象概念,可以使用指定邏輯名稱的自訂組態來建立 HttpClient 執行個體。A factory abstraction for a component that can create HttpClient instances with custom configuration for a given logical name.


元件的 Factory 抽象概念,可以使用指定邏輯名稱的自訂組態來建立 HttpMessageHandler 執行個體。A factory abstraction for a component that can create HttpMessageHandler instances with custom configuration for a given logical name.



指定如何提供用戶端憑證。Specifies how client certificates are provided.


這個列舉可讓您在與伺服器通訊時控制 HTTP Cookie。This enumeration allows control of HTTP cookies when communicating with the server.


表示 HttpClient 作業應該在取得回應之後立即視為已完成,或在讀取整個回應訊息 (包括內容) 之後視為已完成。Indicates if HttpClient operations should be considered completed either as soon as a response is available, or after reading the entire response message including the content.


此列舉會提供在 Windows 上執行時,HttpClient 用於 Proxy 設定的可用選項。This enumeration provides available options for the proxy settings used by an HttpClient when running on Windows.


System.Net.Http 命名空間的設計,是為了提供下列各項:The System.Net.Http namespace is designed to provide the following:

  1. 可讓使用者透過 HTTP 使用新式 web 服務的 HTTP 用戶端元件。HTTP client components that allow users to consume modern web services over HTTP.

  2. 可供用戶端和伺服器使用的 HTTP 元件(例如 HTTP 標頭和訊息)。HTTP components that can be used by both clients and servers (HTTP headers and messages, for example). 這會在用戶端和伺服器端,針對透過 HTTP 的新式 web 服務提供一致的程式設計模型。This provides a consistent programming model on both the client and the server side for modern web services over HTTP.

System.Net.Http 命名空間和相關的 System.Net.Http.Headers 命名空間提供下列元件集:The System.Net.Http namespace and the related System.Net.Http.Headers namespace provide the following set of components:

  1. HttpClient-用來透過 HTTP 傳送和接收要求的主要類別。HttpClient - the primary class used to send and receive requests over HTTP.

  2. HttpRequestMessageHttpResponseMessage-IETF 在 RFC 2616 中定義的 HTTP 訊息。HttpRequestMessage and HttpResponseMessage - HTTP messages as defined in RFC 2616 by the IETF.

  3. HttpHeaders-IETF 在 RFC 2616 中定義的 HTTP 標頭。HttpHeaders - HTTP headers as defined in RFC 2616 by the IETF.

  4. HttpClientHandler-負責產生 HTTP 回應訊息的 HTTP 處理常式。HttpClientHandler - HTTP handlers responsible for producing HTTP response messages.

您可以使用各種不同的 HTTP 訊息處理常式。There are various HTTP message handlers that can be used. 其中包括下列各項。These include the following.

  1. DelegatingHandler-用來將處理常式插入處理常式鏈中的類別。DelegatingHandler - A class used to plug a handler into a handler chain.

  2. HttpMessageHandler-衍生自的簡單類別,可支援大部分應用程式最常見的需求。HttpMessageHandler - A simple to class to derive from that supports the most common requirements for most applications.

  3. HttpClientHandler-在處理常式鏈底部運作的類別,實際上會處理 HTTP 傳輸作業。HttpClientHandler - A class that operates at the bottom of the handler chain that actually handles the HTTP transport operations.

  4. WebRequestHandler-在處理常式鏈類別的底部運作的特殊類別,會使用 System.Net.HttpWebRequest 物件特定的選項來處理 HTTP 傳輸作業。WebRequestHandler - A specialty class that operates at the bottom of the handler chain class that handles HTTP transport operations with options that are specific to the System.Net.HttpWebRequest object.

HTTP 訊息的內容會對應至 RFC 2616 中所定義的實體主體。The contents of an HTTP message corresponds to the entity body defined in RFC 2616.

有一些類別可用於 HTTP 內容。A number of classes can be used for HTTP content. 其中包括下列各項。These include the following.

  1. ByteArrayContent-以位元組陣列為基礎的 HTTP 內容。ByteArrayContent - HTTP content based on a byte array.

  2. FormUrlEncodedContent-使用 application/x-www-表單 urlencoded MIME 類型編碼之名稱/值元組的 HTTP 內容。FormUrlEncodedContent - HTTP content of name/value tuples encoded using application/x-www-form-urlencoded MIME type.

  3. MultipartContent-使用多部分/* 內容類型規格進行序列化的 HTTP 內容。MultipartContent - HTTP content that gets serialized using the multipart/* content type specification.

  4. MultipartFormDataContent-使用多部分/表單資料 MIME 類型編碼的 HTTP 內容。MultipartFormDataContent - HTTP content encoded using the multipart/form-data MIME type.

  5. StreamContent-以資料流程為基礎的 HTTP 內容。StreamContent - HTTP content based on a stream.

  6. StringContent-以字串為基礎的 HTTP 內容。StringContent - HTTP content based on a string.

如果使用 System.Net.HttpSystem.Net.Http.Headers 命名空間的應用程式想要下載大量資料(50 mb 或更多),則應用程式應該串流那些下載,而不是使用預設的緩衝處理。If an app using the System.Net.Http and System.Net.Http.Headers namespaces intends to download large amounts of data (50 megabytes or more), then the app should stream those downloads and not use the default buffering. 如果使用預設緩衝處理,用戶端記憶體使用量就會變得非常大,因此可能會大幅降低效能。If the default buffering is used the client memory usage will get very large, potentially resulting in substantially reduced performance.

System.Net.HttpSystem.Net.Http.Headers 命名空間中的類別,可以用來開發 Windows Store 應用程式或桌面應用程式。Classes in the System.Net.Http and System.Net.Http.Headers namespaces can be used to develop Windows Store apps or desktop apps. 在 Windows Store 應用程式中使用時,System.Net.HttpSystem.Net.Http.Headers 命名空間中的類別會受到網路隔離功能影響,此為 Windows 8Windows 8所使用之應用程式安全性模型的一部分。When used in a Windows Store app, classes in the System.Net.Http and System.Net.Http.Headers namespaces are affected by network isolation feature, part of the application security model used by the Windows 8Windows 8. 您必須在 Windows Store 應用程式的應用程式資訊清單中啟用適當的網路功能,系統才能允許 Windows store 應用程式進行網路存取。The appropriate network capabilities must be enabled in the app manifest for a Windows Store app for the system to allow network access by a Windows store app. 如需詳細資訊,請參閱 Windows 市集應用程式的網路隔離For more information, see the Network Isolation for Windows Store Apps.

See also