style: 优化HttpClient构建过程 (#5357)

This commit is contained in:
ChingCdesu
2023-07-14 10:27:02 +08:00
committed by GitHub

View File

@@ -49,7 +49,6 @@ namespace MaaWpfGui.Services.Web
private readonly ILogger _logger = Log.ForContext<HttpService>();
private HttpClient _client;
private HttpClient _downloader;
public HttpService()
{
@@ -65,12 +64,16 @@ namespace MaaWpfGui.Services.Web
return;
}
BuildDefaultHttpClient();
BuildDownloaderHttpClient();
if (GetProxy() == null)
{
_logger.Warning("Proxy is not a valid URI, and HttpClient is not null, keep using the original HttpClient");
return;
}
_client = BuildHttpClient();
};
BuildDefaultHttpClient();
BuildDownloaderHttpClient();
_client = BuildHttpClient();
}
public async Task<double> HeadAsync(Uri uri, Dictionary<string, string> extraHeader = null)
@@ -88,7 +91,7 @@ namespace MaaWpfGui.Services.Web
}
var stopwatch = Stopwatch.StartNew();
var response = await _client.SendAsync(request);
var response = await _client.SendAsync(request).ConfigureAwait(false);
stopwatch.Stop();
response.Log();
@@ -242,60 +245,29 @@ namespace MaaWpfGui.Services.Web
return success;
}
private void BuildDefaultHttpClient()
private static WebProxy GetProxy()
{
var proxyIsUri = Uri.TryCreate(Proxy, UriKind.RelativeOrAbsolute, out var uri);
proxyIsUri = proxyIsUri && (!string.IsNullOrEmpty(Proxy));
if (proxyIsUri is false)
{
if (!(_client is null))
{
_logger.Information("Proxy is not a valid URI, and Default HttpClient is not null, keep using the original HttpClient");
return;
}
}
_logger.Information("Rebuild Default HttpClient with proxy {Proxy}", Proxy);
var handler = new HttpClientHandler { AllowAutoRedirect = true, };
if (proxyIsUri)
{
handler.Proxy = new WebProxy(uri);
handler.UseProxy = true;
}
_client?.Dispose();
_client = new HttpClient(handler);
_client.DefaultRequestHeaders.Add("User-Agent", UserAgent);
_client.Timeout = TimeSpan.FromSeconds(15);
return (proxyIsUri && (!string.IsNullOrEmpty(Proxy))) is false ? null : new WebProxy(uri);
}
private void BuildDownloaderHttpClient()
private HttpClient BuildHttpClient()
{
var proxyIsUri = Uri.TryCreate(Proxy, UriKind.RelativeOrAbsolute, out var uri);
proxyIsUri = proxyIsUri && (!string.IsNullOrEmpty(Proxy));
if (proxyIsUri is false)
{
if (!(_downloader is null))
{
_logger.Information("Proxy is not a valid URI, and Downloader HttpClient is not null, keep using the original HttpClient");
return;
}
}
_logger.Information("Rebuild Downloader HttpClient with proxy {Proxy}", Proxy);
var handler = new HttpClientHandler { AllowAutoRedirect = true, };
if (proxyIsUri)
var proxy = GetProxy();
if (proxy != null)
{
handler.Proxy = new WebProxy(uri);
_logger.Information("Rebuild HttpClient with proxy {Proxy}", Proxy);
handler.Proxy = proxy;
handler.UseProxy = true;
}
_downloader?.Dispose();
_downloader = new HttpClient(handler);
_downloader.DefaultRequestHeaders.Add("User-Agent", UserAgent);
_downloader.Timeout = TimeSpan.FromMinutes(3);
HttpClient client = new HttpClient(handler);
client = new HttpClient(handler);
client.DefaultRequestHeaders.Add("User-Agent", UserAgent);
client.Timeout = TimeSpan.FromSeconds(15);
return client;
}
}
}