HttpClient 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供一个类,用于从 URI 标识的资源发送 HTTP 请求和接收 HTTP 响应。
public ref class HttpClient : System::Net::Http::HttpMessageInvoker
public class HttpClient : System.Net.Http.HttpMessageInvoker
type HttpClient = class
inherit HttpMessageInvoker
Public Class HttpClient
Inherits HttpMessageInvoker
- 继承
示例
// HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
static readonly HttpClient client = new HttpClient();
static async Task Main()
{
// Call asynchronous network methods in a try/catch block to handle exceptions.
try
{
using HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// Above three lines can be replaced with new helper method below
// string responseBody = await client.GetStringAsync(uri);
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
}
}
open System.Net.Http
// HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
let client = new HttpClient()
let main =
task {
// Call asynchronous network methods in a try/catch block to handle exceptions.
try
use! response = client.GetAsync "http://www.contoso.com/"
response.EnsureSuccessStatusCode() |> ignore
let! responseBody = response.Content.ReadAsStringAsync()
// Above three lines can be replaced with new helper method below
// let! responseBody = client.GetStringAsync uri
printfn $"{responseBody}"
with
| :? HttpRequestException as e ->
printfn "\nException Caught!"
printfn $"Message :{e.Message} "
}
main.Wait()
' HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
Shared ReadOnly client As HttpClient = New HttpClient()
Private Shared Async Function Main() As Task
' Call asynchronous network methods in a try/catch block to handle exceptions.
Try
Using response As HttpResponseMessage = Await client.GetAsync("http://www.contoso.com/")
response.EnsureSuccessStatusCode()
Dim responseBody As String = Await response.Content.ReadAsStringAsync()
' Above three lines can be replaced with new helper method below
' Dim responseBody As String = Await client.GetStringAsync(uri)
Console.WriteLine(responseBody)
End Using
Catch e As HttpRequestException
Console.WriteLine(Environment.NewLine & "Exception Caught!")
Console.WriteLine("Message :{0} ", e.Message)
End Try
End Function
注解
有关此 API 的详细信息,请参阅 HttpClient 的补充 API 说明。
构造函数
HttpClient() |
使用释放此实例时释放的 HttpClientHandler 初始化 HttpClient 类的新实例。 |
HttpClient(HttpMessageHandler) |
使用指定的处理程序初始化 HttpClient 类的新实例。 处理程序在释放此实例时被释放。 |
HttpClient(HttpMessageHandler, Boolean) |
使用提供的处理程序初始化 HttpClient 类的新实例,并指定在释放此实例时是否应释放该处理程序。 |
属性
BaseAddress |
获取或设置发送请求时使用的 Internet 资源的统一资源标识符 (URI) 的基址。 |
DefaultProxy |
获取或设置全局 HTTP 代理。 |
DefaultRequestHeaders |
获取与每个请求一起发送的标题。 |
DefaultRequestVersion |
获取或设置对此 HttpClient 实例发出的后续请求使用的默认 HTTP 版本。 |
DefaultVersionPolicy |
获取或设置便捷方法中隐式创建的请求的默认版本策略,例如 GetAsync(String) 和 PostAsync(String, HttpContent)。 |
MaxResponseContentBufferSize |
获取或设置读取响应内容时要缓冲的最大字节数。 |
Timeout |
获取或设置请求超时前等待的时间跨度。 |