Table of Contents

Class PluginHttpClientBuilder

Namespace
Hi3Helper.Plugin.Core.Utility
Assembly
Hi3Helper.Plugin.Core.dll

Creates an HttpClient based on the customizable settings.

public class PluginHttpClientBuilder
Inheritance
PluginHttpClientBuilder
Inherited Members

Fields

ExDnsResolverWriteBufferLen

public const int ExDnsResolverWriteBufferLen = 512

Field Value

int

ExHttpTimeoutDefault

public const double ExHttpTimeoutDefault = 90

Field Value

double

ExMaxConnectionsDefault

public const int ExMaxConnectionsDefault = 32

Field Value

int

Properties

DecompressionMethod

public DecompressionMethods DecompressionMethod { get; set; }

Property Value

DecompressionMethods

ExternalProxy

public static WebProxy? ExternalProxy { get; }

Property Value

WebProxy

HttpAuthHeader

public string? HttpAuthHeader { get; set; }

Property Value

string

HttpBaseUri

public Uri? HttpBaseUri { get; set; }

Property Value

Uri

HttpHeaders

public Dictionary<string, string?> HttpHeaders { get; set; }

Property Value

Dictionary<string, string>

HttpProtocolVersion

public Version HttpProtocolVersion { get; set; }

Property Value

Version

HttpProtocolVersionPolicy

public HttpVersionPolicy HttpProtocolVersionPolicy { get; set; }

Property Value

HttpVersionPolicy

HttpTimeout

public TimeSpan HttpTimeout { get; set; }

Property Value

TimeSpan

HttpUserAgent

public string? HttpUserAgent { get; set; }

Property Value

string

IsAllowHttpCookies

public bool IsAllowHttpCookies { get; set; }

Property Value

bool

IsAllowHttpRedirections

public bool IsAllowHttpRedirections { get; set; }

Property Value

bool

IsAllowUntrustedCert

public bool IsAllowUntrustedCert { get; set; }

Property Value

bool

IsUseProxy

public static bool IsUseProxy { get; }

Property Value

bool

IsUseSystemProxy

public static bool IsUseSystemProxy { get; }

Property Value

bool

MaxConnections

public int MaxConnections { get; set; }

Property Value

int

Methods

AddHeader(string, string?)

Adds or updates an HTTP header in the builder's configuration.

public PluginHttpClientBuilder AddHeader(string key, string? value)

Parameters

key string

The name of the HTTP header to add or update. Cannot be null or empty.

value string

The value of the HTTP header. Can be null to indicate the header should have no value.

Returns

PluginHttpClientBuilder

Remarks

If the header key is "User-Agent" (case-insensitive), the value of the HttpUserAgent property will be overridden.
If the specified header key already exists, its value will be updated; otherwise, the header will be added.

AllowCookies(bool)

Allows cookies to be used on the HttpClient or not.

public PluginHttpClientBuilder AllowCookies(bool allowCookies = true)

Parameters

allowCookies bool

Either allow cookies or not.

Returns

PluginHttpClientBuilder

Remarks

Default: true

AllowRedirections(bool)

Allows the redirection (30x) HTTP status code or not.

public PluginHttpClientBuilder AllowRedirections(bool allowRedirections = true)

Parameters

allowRedirections bool

Either allow the redirection or not.

Returns

PluginHttpClientBuilder

Remarks

Default: true

AllowUntrustedCert(bool)

Allows HttpClient to skip untrusted/unvalidated certificate validation on each of the request.

public PluginHttpClientBuilder AllowUntrustedCert(bool allowUntrustedCert = true)

Parameters

allowUntrustedCert bool

Either to allow HttpClient to skip certificate validation or not.

Returns

PluginHttpClientBuilder

Remarks

Default: false

Create()

Creates and configures an instance of HttpClient with the specified settings.

public HttpClient Create()

Returns

HttpClient

A fully configured HttpClient instance ready for making HTTP requests.

Remarks

This method initializes a new HttpClient using a SocketsHttpHandler configured with various properties such as proxy settings, connection limits, decompression methods, and HTTP protocol versions. It also applies custom headers, authentication settings, and other options based on the provided configuration. The returned HttpClient is ready for use and includes settings such as timeout, base address, and user-agent headers. If the configuration allows untrusted certificates, the handler is set to bypass certificate validation.

SetAllowedDecompression(DecompressionMethods)

Sets the allowed decompression methods on the HTTP response.

public PluginHttpClientBuilder SetAllowedDecompression(DecompressionMethods decompressionMethods = (DecompressionMethods)-1)

Parameters

decompressionMethods DecompressionMethods

Kind of decompressors to be used.

Returns

PluginHttpClientBuilder

Remarks

Default: All

SetAuthHeader(string?)

Sets the value of Authorization HTTP header.

public PluginHttpClientBuilder SetAuthHeader(string? authHeader = null)

Parameters

authHeader string

Returns

PluginHttpClientBuilder

Remarks

Default: null
If the authHeader is set to null, the Authorization HTTP header will be unset.

SetBaseUrl(string)

Sets the base URL used by the HttpClient.

public PluginHttpClientBuilder SetBaseUrl(string baseUrl)

Parameters

baseUrl string

The base URL used by the HttpClient.

Returns

PluginHttpClientBuilder

SetBaseUrl(Uri)

Sets the base URL used by the HttpClient.

public PluginHttpClientBuilder SetBaseUrl(Uri baseUrl)

Parameters

baseUrl Uri

The base URL used by the HttpClient.

Returns

PluginHttpClientBuilder

SetHttpVersion(Version?, HttpVersionPolicy)

Sets the policy of which version of HTTP to be used.

public PluginHttpClientBuilder SetHttpVersion(Version? version = null, HttpVersionPolicy versionPolicy = HttpVersionPolicy.RequestVersionOrLower)

Parameters

version Version

What HTTP version to be requested.

versionPolicy HttpVersionPolicy

The version request policy used.

Returns

PluginHttpClientBuilder

Remarks

Default version: null (or Version11)
Default versionPolicy: RequestVersionOrLower

If the version is set to null, the Version11 will be used instead.
If MANUALCOM constant is defined and version set to Version30, it will be reverted to Version20 (due to unsupported features).

SetMaxConnection(int)

Sets the maximum connection to be established by the HttpClient.

public PluginHttpClientBuilder SetMaxConnection(int maxConnections = 32)

Parameters

maxConnections int

The amount of connections to be handled.

Returns

PluginHttpClientBuilder

Remarks

Minimum: 2
Default: ExMaxConnectionsDefault

SetTimeout(double)

Sets how long the request will be allowed to be performed until certain timeout duration (in seconds).

public PluginHttpClientBuilder SetTimeout(double fromSeconds = 90)

Parameters

fromSeconds double

How many seconds before timeout is happening.

Returns

PluginHttpClientBuilder

Remarks

SetTimeout(TimeSpan?)

Sets how long the request will be allowed to be performed until certain timeout duration (in TimeSpan).

public PluginHttpClientBuilder SetTimeout(TimeSpan? timeout = null)

Parameters

timeout TimeSpan?

The amount of time span used before timeout is happening.

Returns

PluginHttpClientBuilder

Remarks

Default: TimeSpan.FromSeconds(ExHttpTimeoutDefault)

SetUserAgent(string?)

Sets the user-agent used by the HttpClient on each of its requests.

public PluginHttpClientBuilder SetUserAgent(string? userAgent = null)

Parameters

userAgent string

The user-agent to be used.

Returns

PluginHttpClientBuilder

Remarks

Default: null
If the userAgent is set to null, the default plugin user-agent will be used instead.