在HttpClient中设置HTTP协议版本

人气:555 发布:2022-09-21 标签: http c# .net-4.5 httpclient

问题描述

我需要向使用HTTP 1.0版的Web服务发出请求。我使用 HttpClient ,但我看不到设置HTTP版本的任何选项。

I need to make a request to a webservice that uses HTTP version 1.0. Im using HttpClient , But I cant see any option to set HTTP version.

我在哪里可以设置请求版本?

Where can i set the request version?

推荐答案

为了设置版本,您必须创建 HttpRequestMessage 并设置其传递给 HttpClient.SendAsync 。您可以使用帮助 HttpVersion 实用程序类:

In order to set the version you'll have to create an instance of HttpRequestMessage and set its Version property which you pass to HttpClient.SendAsync. You can use the helper HttpVersion utility class:

var requestMessage = new HttpRequestMessage 
{
    Version = HttpVersion.Version10
}; 

var client = new HttpClient();
var response = await client.SendAsync(requestMessage);

738