System.Net.Http Namespace

System.Net.Http 命名空间提供用于现代 HTTP 应用程序的编程接口。 The System.Net.Http namespace provides a programming interface for modern HTTP applications.

Classes

ByteArrayContent

根据字节数组提供 HTTP 内容。Provides HTTP content based on a byte array.

CFNetworkHandler
DelegatingHandler

将 HTTP 响应消息的处理委托给另一处理程序(称为“内部处理程序”)的 HTTP 处理程序的类型。A type for HTTP handlers that delegate the processing of HTTP response messages to another handler, called the inner handler.

FormUrlEncodedContent

使用 application/x-www-form-urlencoded MIME 类型编码的名称/值元组的容器。A container for name/value tuples encoded using application/x-www-form-urlencoded MIME type.

HttpClient

提供用于发送 HTTP 请求并从 URI 标识的资源接收 HTTP 响应的基类。Provides a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI.

HttpClientFactoryExtensions

IHttpClientFactory 的扩展方法。Extensions methods for IHttpClientFactory.

HttpClientHandler

.NET Framework 和 .NET Core 2.0 及更低版本中由 HttpClient 使用的默认消息处理程序。The default message handler used by HttpClient in .NET Framework and .NET Core 2.0 and earlier.

HttpContent

表示 HTTP 实体正文和内容标头的基类。A base class representing an HTTP entity body and content headers.

HttpMessageHandler

HTTP 消息处理程序的基类型。A base type for HTTP message handlers.

HttpMessageHandlerFactoryExtensions

IHttpMessageHandlerFactory 的扩展方法。Extensions methods for IHttpMessageHandlerFactory.

HttpMessageInvoker

一个特殊类,它允许应用程序对 HTTP 处理程序链调用 SendAsync(HttpRequestMessage, CancellationToken) 方法。A specialty class that allows applications to call the SendAsync(HttpRequestMessage, CancellationToken) method on an HTTP handler chain.

HttpMethod

一个帮助器类,它用于检索并比较标准 HTTP 方法并且用于创建新的 HTTP 方法。A helper class for retrieving and comparing standard HTTP methods and for creating new HTTP methods.

HttpRequestException

HttpClientHttpMessageHandler 所引发的异常的基类。A base class for exceptions thrown by the HttpClient and HttpMessageHandler classes.

HttpRequestMessage

表示 HTTP 请求消息。Represents a HTTP request message.

HttpResponseMessage

表示包括状态代码和数据的 HTTP 响应消息。Represents a HTTP response message including the status code and data.

MessageProcessingHandler

只对请求和/或响应消息进行一些简要处理的处理程序的基类型。A base type for handlers which only do some small processing of request and/or response messages.

MultipartContent

提供 HttpContent 对象的集合,其可通过使用多部分/* 内容类型规范序列化。Provides a collection of HttpContent objects that get serialized using the multipart/* content type specification.

MultipartFormDataContent

为使用 multipart/form-data MIME 类型编码的内容提供容器。Provides a container for content encoded using multipart/form-data MIME type.

NSUrlSessionHandler

通过 Xamarin 堆栈在 Apple 平台上由  HttpClient 使用的默认消息处理程序(iOS、macOS、watchOS、tvOS)The default message handler used by HttpClient on Apple platforms using the Xamarin stack (iOS, macOS, watchOS, tvOS)

ReadOnlyMemoryContent

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

RtcRequestFactory
SocketsHttpHandler

提供在 .NET Core 2.1 及更高版本中由 HttpClient 使用的默认消息处理程序。Provides the default message handler used by HttpClient in .NET Core 2.1 and later.

StreamContent

根据流提供 HTTP 内容。Provides HTTP content based on a stream.

StringContent

根据字符串提供 HTTP 内容。Provides HTTP content based on a string.

WebRequestHandler

提供不可用于 Windows Store 应用程序或其他环境的特定以桌面的功能。Provides desktop-specific features not available to Windows Store apps or other environments.

WinHttpHandler

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.

Interfaces

IHttpClientFactory

组件的工厂抽象,该组件可使用自定义配置为给定逻辑名称创建 HttpClient 实例。A factory abstraction for a component that can create HttpClient instances with custom configuration for a given logical name.

IHttpMessageHandlerFactory

组件的工厂抽象,该组件可使用自定义配置为给定逻辑名称创建 HttpMessageHandler 实例。A factory abstraction for a component that can create HttpMessageHandler instances with custom configuration for a given logical name.

Enums

ClientCertificateOption

指定提供客户端证书的方式。Specifies how client certificates are provided.

CookieUsePolicy

与服务器通信时,此枚举允许控制 HTTP cookie。This enumeration allows control of HTTP cookies when communicating with the server.

HttpCompletionOption

指示 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.

WindowsProxyUsePolicy

在 Windows 上运行时,此枚举提供 HttpClient 所使用的代理设置的可用选项。This enumeration provides available options for the proxy settings used by an HttpClient when running on Windows.

Remarks

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. 由 IETF 在 RFC 2616 中定义的 HttpRequestMessageHttpResponseMessage 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-url 编码 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.Http 的应用程序和 System.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 应用商店应用程序或桌面应用程序。Classes in the System.Net.Http and System.Net.Http.Headers namespaces can be used to develop Windows Store apps or desktop apps. 在 Windows 应用商店应用程序中使用时,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 应用商店应用程序的应用程序清单中启用相应的网络功能,系统才能允许 Windows 应用商店应用程序进行网络访问。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 Store 应用的网络隔离For more information, see the Network Isolation for Windows Store Apps.

See also