Add UseWinHttpHandler

This commit is contained in:
Aigio Liu
2022-07-24 13:41:29 +08:00
parent 4826939668
commit efae85db2c
10 changed files with 164 additions and 24 deletions
+1
View File
@@ -84,6 +84,7 @@
<!--<PackageVersion Include="CommunityToolkit.Mvvm" Version="7.1.2" />-->
<PackageVersion Include="System.Text.Encoding.CodePages" Version="6.0.0" />
<PackageVersion Include="System.Diagnostics.PerformanceCounter" Version="6.0.1" />
<PackageVersion Include="System.Net.Http.WinHttpHandler" Version="6.0.1" />
<!-- AppCenter.* -->
<PackageVersion Include="Microsoft.AppCenter" Version="4.5.1" />
@@ -14,10 +14,10 @@ namespace System.Application.Services.Implementation
#endif
}
public static AndroidClientHandler CreateAndroidClientHandler()
public static AndroidClientHandler CreateAndroidClientHandler(AndroidClientHandler? handler = null)
{
// https://docs.microsoft.com/zh-cn/xamarin/android/app-fundamentals/http-stack?context=xamarin%2Fcross-platform&tabs=macos
var handler = new AndroidClientHandler();
handler ??= new AndroidClientHandler();
AddFiddlerRootCertificate(handler);
GeneralHttpClientFactory.SetProxyToHandler(GeneralHttpClientFactory.DefaultProxy, handler);
return handler;
@@ -69,6 +69,18 @@
</DockPanel>
</Border>
<Border Classes="Item" IsVisible="{markup:OnPlatform Windows}">
<DockPanel>
<StackPanel>
<TextBlock Text="{ReflectionBinding Path=Res.Settings_General_UseWinHttpHandler,Mode=OneWay,Source={x:Static resx:R.Current}}"/>
<TextBlock Classes="prompt"
Text="{ReflectionBinding Path=Res.Settings_RestartEffective,Mode=OneWay,Source={x:Static resx:R.Current}}"/>
</StackPanel>
<ToggleSwitch IsChecked="{ReflectionBinding Source={x:Static ms:GeneralSettings.UseWinHttpHandler},Path=Value, Mode=TwoWay}"/>
</DockPanel>
</Border>
<!--<Border Classes="Item"
IsVisible="{markup:OnPlatform Windows}">
<DockPanel>
@@ -37,6 +37,7 @@
<PackageReference Include="7-Zip.NativeAssets.Win32" />
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" />
<PackageReference Include="System.Management" />
<PackageReference Include="System.Net.Http.WinHttpHandler" />
</ItemGroup>
<!--<ItemGroup Condition=" !$(TargetFramework.Contains('-windows')) ">
@@ -18,8 +18,8 @@ using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using MSEXLog = Microsoft.Extensions.Logging;
using CC = System.Common.Constants;
using MSEXLog = Microsoft.Extensions.Logging;
namespace System.Application.Services.Implementation
{
@@ -113,5 +113,15 @@ namespace System.Application.Settings
///// </summary>
//public static SerializableProperty<bool> IsEnableLogRecord { get; }
// = GetProperty(defaultValue: false, autoSave: true);
static readonly SerializableProperty<bool>? _UseWinHttpHandler = OperatingSystem2.IsWindows() ? GetProperty(defaultValue: false, autoSave: true) : null;
/// <summary>
/// (仅 Windows)使用基于 Windows 的 WinHTTP 接口处理消息
/// <para>https://docs.microsoft.com/zh-cn/dotnet/api/system.net.http.winhttphandler?view=dotnet-plat-ext-6.0</para>
/// <para>https://docs.microsoft.com/zh-cn/dotnet/api/system.net.http.socketshttphandler?view=net-6.0</para>
/// </summary>
[SupportedOSPlatform("Windows7.0")]
public static SerializableProperty<bool> UseWinHttpHandler => _UseWinHttpHandler ?? throw new PlatformNotSupportedException();
}
}
+9
View File
@@ -5140,6 +5140,15 @@ namespace System.Application.UI.Resx {
}
}
/// <summary>
/// 查找类似 使用 WinHttpHandler 的本地化字符串。
/// </summary>
public static string Settings_General_UseWinHttpHandler {
get {
return ResourceManager.GetString("Settings_General_UseWinHttpHandler", resourceCulture);
}
}
/// <summary>
/// 查找类似 语言 的本地化字符串。
/// </summary>
+3
View File
@@ -2380,4 +2380,7 @@ AAAAA-BBBBB-CCCC3</value>
<data name="Notice_Type" xml:space="preserve">
<value>消息类型</value>
</data>
<data name="Settings_General_UseWinHttpHandler" xml:space="preserve">
<value>使用 WinHttpHandler</value>
</data>
</root>
@@ -16,6 +16,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// <typeparam name="T"></typeparam>
/// <param name="services"></param>
/// <param name="config"></param>
/// <param name="configureHandler"></param>
/// <param name="useMock"></param>
/// <returns></returns>
public static IServiceCollection TryAddCloudServiceClient<T>(
+124 -21
View File
@@ -72,6 +72,12 @@ using LogLevel = Microsoft.Extensions.Logging.LogLevel;
#pragma warning restore SA1216 // Using static directives should be placed at the correct location
#pragma warning restore SA1211 // Using alias directives should be ordered alphabetically by alias name
#pragma warning restore IDE0079 // 请删除不必要的忽略
using CreateHttpHandlerArgs = System.ValueTuple<
bool,
System.Net.DecompressionMethods,
System.Net.CookieContainer,
System.Net.IWebProxy?,
int>;
namespace System.Application.UI
{
@@ -429,32 +435,12 @@ namespace System.Application.UI
services.TryAddModelValidator();
// 添加服务端API调用
#pragma warning disable SA1111 // Closing parenthesis should be on line of last parameter
services.TryAddCloudServiceClient<CloudServiceClient>(c =>
{
#if NETCOREAPP3_0_OR_GREATER
c.DefaultRequestVersion = HttpVersion.Version30;
#endif
}, configureHandler:
#if NETCOREAPP2_1_OR_GREATER
() => new SocketsHttpHandler
{
UseCookies = false,
AutomaticDecompression = DecompressionMethods.Brotli | DecompressionMethods.GZip,
}
#elif __ANDROID__
() =>
{
var handler = PlatformHttpMessageHandlerBuilder.CreateAndroidClientHandler();
handler.UseCookies = false;
handler.AutomaticDecompression = DecompressionMethods.GZip;
return handler;
}
#else
null
#endif
);
#pragma warning restore SA1111 // Closing parenthesis should be on line of last parameter
}, configureHandler: ConfigureHandler());
services.AddAutoMapper();
@@ -629,6 +615,8 @@ namespace System.Application.UI
{
if (isTrace) StartWatchTrace.Record(dispose: true);
}
ArchiSteamFarm.Web.WebBrowser.CreateHttpHandlerDelegate = CreateHttpHandler;
}
/// <summary>
@@ -897,5 +885,120 @@ namespace System.Application.UI
#endif
#endregion
#region HttpMessageHandler
static Func<HttpMessageHandler>? ConfigureHandler()
{
#if NETCOREAPP2_1_OR_GREATER
#if WINDOWS
if (GeneralSettings.UseWinHttpHandler.Value)
{
return () => new WinHttpHandler
{
WindowsProxyUsePolicy = WindowsProxyUsePolicy.DoNotUseProxy,
CookieUsePolicy = CookieUsePolicy.IgnoreCookies,
AutomaticDecompression = DecompressionMethods.Brotli | DecompressionMethods.GZip,
};
}
else
#endif
{
return () => new SocketsHttpHandler
{
UseCookies = false,
AutomaticDecompression = DecompressionMethods.Brotli | DecompressionMethods.GZip,
};
}
#elif __ANDROID__
return () =>
{
var handler = PlatformHttpMessageHandlerBuilder.CreateAndroidClientHandler();
handler.UseCookies = false;
handler.AutomaticDecompression = DecompressionMethods.GZip;
return handler;
};
#else
return null;
#endif
}
static HttpMessageHandler? CreateHttpHandler(CreateHttpHandlerArgs args)
{
#if NETCOREAPP2_1_OR_GREATER
#if WINDOWS
if (GeneralSettings.UseWinHttpHandler.Value)
{
var handler = new WinHttpHandler
{
AutomaticRedirection = args.Item1,
AutomaticDecompression = args.Item2,
CookieContainer = args.Item3,
};
var proxy = args.Item4;
var useProxy = GeneralHttpClientFactory.UseWebProxy(proxy);
if (useProxy)
{
handler.Proxy = proxy;
handler.WindowsProxyUsePolicy = WindowsProxyUsePolicy.UseCustomProxy;
}
else
{
handler.WindowsProxyUsePolicy = WindowsProxyUsePolicy.DoNotUseProxy;
}
if (!(args.Item5 < 1)) // https://github.com/dotnet/runtime/blob/v6.0.0/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs#L157
{
handler.MaxConnectionsPerServer = args.Item5;
}
return handler;
}
else
#endif
{
var handler = new SocketsHttpHandler
{
AllowAutoRedirect = args.Item1,
AutomaticDecompression = args.Item2,
CookieContainer = args.Item3,
};
var proxy = args.Item4;
var useProxy = GeneralHttpClientFactory.UseWebProxy(proxy);
if (useProxy)
{
handler.Proxy = proxy;
handler.UseProxy = true;
}
if (!(args.Item5 < 1)) // https://github.com/dotnet/runtime/blob/v6.0.0/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs#L157
{
handler.MaxConnectionsPerServer = args.Item5;
}
return handler;
}
#elif __ANDROID__
var handler = new AndroidClientHandler
{
AllowAutoRedirect = args.Item1,
AutomaticDecompression = args.Item2,
CookieContainer = args.Item3,
};
PlatformHttpMessageHandlerBuilder.CreateAndroidClientHandler(handler);
var proxy = args.Item4;
var useProxy = GeneralHttpClientFactory.UseWebProxy(proxy);
if (useProxy)
{
handler.Proxy = proxy;
handler.UseProxy = true;
}
if (!(args.Item5 < 1)) // https://github.com/dotnet/runtime/blob/v6.0.0/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs#L157
{
handler.MaxConnectionsPerServer = args.Item5;
}
return handler;
#else
return null;
#endif
}
#endregion
}
}