Skip to content

Commit

Permalink
优化http请求
Browse files Browse the repository at this point in the history
  • Loading branch information
MonoLogueChi committed Apr 15, 2020
1 parent 3b92349 commit 7ef735b
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 30 deletions.
1 change: 1 addition & 0 deletions Danmu.sln.DotSettings.user
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<Assembly Path="C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.1.15\System.Private.CoreLib.dll" />
<Assembly Path="C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.2\System.Net.Http.dll" />
<Assembly Path="C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.2\System.Private.CoreLib.dll" />
<Assembly Path="C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\3.1.3\Microsoft.Extensions.Http.dll" />
&lt;/AssemblyExplorer&gt;</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=artplayer/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Bili/@EntryIndexedValue">True</s:Boolean>
Expand Down
4 changes: 3 additions & 1 deletion Danmu/Danmu.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
</ItemGroup>

<ItemGroup>
<TrimmerRootDescriptor Include="TrimmerRoots.xml" />
<Content Update="runtimeconfig.template.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
7 changes: 3 additions & 4 deletions Danmu/Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
<ExcludeApp_Data>False</ExcludeApp_Data>
<TargetFramework>netcoreapp3.1</TargetFramework>
<ProjectGuid>b9c588d4-1f1b-47c6-8dfd-8434978890a7</ProjectGuid>
<SelfContained>true</SelfContained>
<SelfContained>false</SelfContained>
<publishUrl>bin\Release-Win\netcoreapp3.1\publish\</publishUrl>
<DeleteExistingFiles>False</DeleteExistingFiles>
<DeleteExistingFiles>True</DeleteExistingFiles>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishSingleFile>True</PublishSingleFile>
<PublishTrimmed>True</PublishTrimmed>
<PublishSingleFile>False</PublishSingleFile>
</PropertyGroup>
</Project>
3 changes: 2 additions & 1 deletion Danmu/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void ConfigureServices(IServiceCollection services)


//Http请求
services.AddHttpClient();
services.AddHttpClient(Gzip).ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler
{AutomaticDecompression = DecompressionMethods.GZip});
services.AddHttpClient(Deflate).ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler
Expand Down Expand Up @@ -151,7 +152,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
#if DEBUG
spa.Options.SourcePath = "ClientApp";
spa.UseVueCli("serve",18081);
spa.UseVueCli("serve", 18081);
#endif
});
}
Expand Down
17 changes: 0 additions & 17 deletions Danmu/TrimmerRoots.xml

This file was deleted.

12 changes: 7 additions & 5 deletions Danmu/Utils/BiliBili/BiliBiliHelp.Http.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Danmu.Utils.Common;
using static Danmu.Utils.Global.VariableDictionary;
Expand All @@ -17,8 +18,8 @@ private async Task<byte[]> GetBiliBiliPageRawAsync(string url)
var key = Md5.GetMd5(url);
return await _cache.GetOrCreateHttpCacheAsync(key, TimeSpan.FromMinutes(_setting.CidCacheTime), async () =>
{
var httpClient = _httpClientFactory.CreateClient(Deflate);
var response = await httpClient.GetAsync(url);
var client = _httpClientFactory.CreateClient(Deflate);
var response = await client.GetAsync(url);
if (response.IsSuccessStatusCode) return await response.Content.ReadAsByteArrayAsync();
return new byte[0];
});
Expand All @@ -35,10 +36,11 @@ private async Task<byte[]> GetDanmuRawAsync(string url, bool useCookie = false)
var key = Md5.GetMd5(url);
return await _cache.GetOrCreateHttpCacheAsync(key, TimeSpan.FromMinutes(_setting.DanmuCacheTime), async () =>
{
var deflateClient = _httpClientFactory.CreateClient(Deflate);
var request = new HttpRequestMessage(HttpMethod.Get, url);
if (useCookie && !string.IsNullOrEmpty(_setting.Cookie))
deflateClient.DefaultRequestHeaders.Add("Cookie", _setting.Cookie);
var response = await deflateClient.GetAsync(url);
request.Headers.Add("Cookie", _setting.Cookie);
var client = _httpClientFactory.CreateClient(Deflate);
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode) return await response.Content.ReadAsByteArrayAsync();
return new byte[0];
});
Expand Down
4 changes: 2 additions & 2 deletions Danmu/Utils/BiliBili/BiliBiliHelp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public partial class BiliBiliHelp
private readonly IHttpClientFactory _httpClientFactory;
private readonly BiliBiliSetting _setting;

public BiliBiliHelp(AppConfiguration appConfiguration, IHttpClientFactory httpClientFactory,
public BiliBiliHelp(AppConfiguration appConfiguration, IHttpClientFactory clientFactory,
CacheDao biliBiliCache)
{
_httpClientFactory = httpClientFactory;
_httpClientFactory = clientFactory;
_setting = appConfiguration.GetAppSetting().BiliBiliSetting;
_canGetHistory = !string.IsNullOrEmpty(_setting.Cookie);
_cache = biliBiliCache;
Expand Down
5 changes: 5 additions & 0 deletions Danmu/runtimeconfig.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"configProperties": {
"System.Net.Http.SocketsHttpHandler.Http2Support": true
}
}

0 comments on commit 7ef735b

Please sign in to comment.