5.28Configuring the HTTP Version
When initiating HTTP remote requests, the default HTTP protocol version is 1.1. However, when accessing certain third-party servers, these servers may validate the HTTP version (for example, requiring version 2.0).
HTTP Declarative Requests set the HTTP version through the HttpVersionAttribute attribute. The corresponding HTTP declarative extractor is implemented as the HttpVersionDeclarativeExtractor type, which is responsible for parsing the HttpVersionAttribute attribute and building the HTTP version configuration required by an HttpRequestBuilder instance.
// Apply at the interface level to affect all methods[HttpVersion("1.2")]public interface IHttpService : IHttpDeclarative{ [Get("https://furion.net/logo.png")] Task<string> GetStringAsync(); // Apply at the method level [HttpVersion("2.0")] [Get("https://furion.net/logo2.png")] Task<string> GetStringAsync();}Once enabled, the HTTP version is automatically set when sending HTTP remote requests.
In addition to configuring it through the [HttpVersion] attribute, the system also supports a global configuration approach, as shown in the following example:
// Configure the default clientservices.AddHttpClient(string.Empty, client =>{ client.DefaultRequestVersion = HttpVersion.Version10;});// Configure a specific clientservices.AddHttpClient("weixin", client =>{ client.DefaultRequestVersion = HttpVersion.Version10;});HttpVersionAttribute includes the following constructors and properties:
-
Constructors:
new(version): Applies to methods or interfaces, setting theHTTPversion.
-
Properties:
Version: TheHTTPversion (typestring).