问题描述
是否可以在 .NET 4.5 中使用新的 HttpClient
创建 HTTP HEAD 请求?我能找到的唯一方法是 GetAsync
、DeleteAsync
、PutAsync
和 PostAsync
.我知道 HttpWebRequest
类能够做到这一点,但我想使用现代的 HttpClient
.
Is it possible to create a HTTP HEAD request with the new HttpClient
in .NET 4.5? The only methods I can find are GetAsync
, DeleteAsync
, PutAsync
and PostAsync
. I know that the HttpWebRequest
-class is able to do that, but I want to use the modern HttpClient
.
推荐答案
将 SendAsync
方法与使用 HttpMethod.Head 构造的
.HttpRequestMessage
实例一起使用
Use the SendAsync
method with an instance of HttpRequestMessage
that was constructed using HttpMethod.Head
.
GetAsync
、PostAsync
等是 方便的包装器围绕SendAsync
;不太常见的 HTTP 方法,例如 HEAD
、OPTIONS
等,没有包装器.
GetAsync
, PostAsync
, etc are convenient wrappers around SendAsync
; the less common HTTP methods such as HEAD
, OPTIONS
, etc, don't get a wrapper.
简而言之:
client.SendAsync(new HttpRequestMessage(HttpMethod.Head, url))
这篇关于.NET 4.5 和 C# 中带有 HttpClient 的 HTTP HEAD 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!