diff --git a/ExClient/Client-User.cs b/ExClient/Client-User.cs index 2446db6f..35186ffd 100644 --- a/ExClient/Client-User.cs +++ b/ExClient/Client-User.cs @@ -261,11 +261,11 @@ internal string PassHash } } - public Task FetchCurrentUserInfoAsync() + public async Task FetchCurrentUserInfoAsync() { if (UserId < 0) throw new InvalidOperationException("Hasn't log in"); - return UserInfo.FeachAsync(UserId); + return await UserInfo.FetchAsync(UserId); } } -} \ No newline at end of file +} diff --git a/ExClient/Forums/UserInfo.cs b/ExClient/Forums/UserInfo.cs index 6ef3cead..5c688522 100644 --- a/ExClient/Forums/UserInfo.cs +++ b/ExClient/Forums/UserInfo.cs @@ -24,7 +24,7 @@ public class UserInfo /// /// Fetch user info from forum.e-hentai.org/index?showuser={}. /// - public static Task FeachAsync(long userID, CancellationToken token = default) + public static Task FetchAsync(long userID, CancellationToken token = default) { if (userID <= 0) throw new ArgumentOutOfRangeException(nameof(userID)); @@ -112,4 +112,4 @@ private UserInfo() { } [JsonProperty] public Uri Avatar { get; private set; } } -} \ No newline at end of file +} diff --git a/ExClient/Internal/MyHttpClient.cs b/ExClient/Internal/MyHttpClient.cs index 8096a40a..258108c7 100644 --- a/ExClient/Internal/MyHttpClient.cs +++ b/ExClient/Internal/MyHttpClient.cs @@ -5,7 +5,8 @@ using System; using System.Collections.Generic; - +using System.IO; +using System.Text; using Windows.ApplicationModel; using Windows.Foundation; using Windows.Storage.Streams; @@ -17,15 +18,12 @@ using IHttpAsyncOperation = Windows.Foundation.IAsyncOperationWithProgress; -namespace ExClient.Internal -{ +namespace ExClient.Internal { /* * 由于使用了自定义 Filter 后发生异常会丢失异常详细信息,故使用此类封装,以保留异常信息。 * */ - internal class MyHttpClient : IDisposable - { - private void _CheckStringResponse(string responseString) - { + internal class MyHttpClient : IDisposable { + private void _CheckStringResponse(string responseString) { if (responseString.Length > 200) return; if (responseString.Contains("This gallery is currently unavailable.")) @@ -34,28 +32,23 @@ private void _CheckStringResponse(string responseString) throw new InvalidOperationException(LocalizedStrings.Resources.IPBannedOfPageLoad); if (responseString.Contains("This page is currently not available, as your account has been suspended.")) throw new InvalidOperationException(LocalizedStrings.Resources.AccountSuspended); - if (responseString.Contains("https://exhentai.org/img/kokomade.jpg")) - { + if (responseString.Contains("https://exhentai.org/img/kokomade.jpg")) { _ = _Owner.ResetExCookieAsync(); throw new InvalidOperationException(LocalizedStrings.Resources.Kokomade); } } - private void _CheckSadPanda(HttpResponseMessage response) - { - if (response.Content.Headers.ContentDisposition?.FileName == "sadpanda.jpg") - { + private void _CheckSadPanda(HttpResponseMessage response) { + if (response.Content.Headers.ContentDisposition?.FileName == "sadpanda.jpg") { _ = _Owner.ResetExCookieAsync(); throw new InvalidOperationException(LocalizedStrings.Resources.SadPanda); } } - public MyHttpClient(Client owner, HttpClient inner) - { + public MyHttpClient(Client owner, HttpClient inner) { _Inner = inner; _Owner = owner; - _Nocookie = new HttpClient(new HttpBaseProtocolFilter - { + _Nocookie = new HttpClient(new HttpBaseProtocolFilter { CookieUsageBehavior = HttpCookieUsageBehavior.NoCookies, }); @@ -64,8 +57,7 @@ public MyHttpClient(Client owner, HttpClient inner) _Nocookie.DefaultRequestHeaders.UserAgent.Add(ua); } - private void _ReformUri(ref Uri uri) - { + private void _ReformUri(ref Uri uri) { if (!uri.IsAbsoluteUri) uri = new Uri(_Owner.Uris.RootUri, uri); } @@ -76,37 +68,30 @@ private void _ReformUri(ref Uri uri) public HttpRequestHeaderCollection DefaultRequestHeaders => _Inner.DefaultRequestHeaders; - public IHttpAsyncOperation GetAsync(Uri uri, HttpCompletionOption completionOption, bool checkStatusCode) - { + public IHttpAsyncOperation GetAsync(Uri uri, HttpCompletionOption completionOption, bool checkStatusCode) { _ReformUri(ref uri); var client = uri.Host.EndsWith("hentai.org") ? _Inner : _Nocookie; var request = client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead); - if (completionOption == HttpCompletionOption.ResponseHeadersRead) - { + if (completionOption == HttpCompletionOption.ResponseHeadersRead) { return request; } - return Run(async (token, progress) => - { + return Run(async (token, progress) => { token.Register(request.Cancel); request.Progress = (t, p) => progress.Report(p); var response = await request; _CheckSadPanda(response); - if (checkStatusCode) - { + if (checkStatusCode) { response.EnsureSuccessStatusCode(); } var buffer = response.Content.BufferAllAsync(); - if (!response.Content.TryComputeLength(out var length)) - { + if (!response.Content.TryComputeLength(out var length)) { var contentLength = response.Content.Headers.ContentLength; length = contentLength ?? ulong.MaxValue; } - buffer.Progress = (t, p) => - { - progress.Report(new HttpProgress - { + buffer.Progress = (t, p) => { + progress.Report(new HttpProgress { TotalBytesToReceive = length, BytesReceived = p, Stage = HttpProgressStage.ReceivingContent @@ -117,16 +102,13 @@ public IHttpAsyncOperation GetAsync(Uri uri, HttpCompletionOption completionOpti }); } - public IHttpAsyncOperation GetAsync(Uri uri) - { + public IHttpAsyncOperation GetAsync(Uri uri) { return GetAsync(uri, HttpCompletionOption.ResponseContentRead, true); } - public IAsyncOperationWithProgress GetBufferAsync(Uri uri) - { + public IAsyncOperationWithProgress GetBufferAsync(Uri uri) { _ReformUri(ref uri); - return Run(async (token, progress) => - { + return Run(async (token, progress) => { var request = GetAsync(uri); token.Register(request.Cancel); request.Progress = (t, p) => progress.Report(p); @@ -135,11 +117,9 @@ public IAsyncOperationWithProgress GetBufferAsync(Uri uri }); } - public IAsyncOperationWithProgress GetInputStreamAsync(Uri uri) - { + public IAsyncOperationWithProgress GetInputStreamAsync(Uri uri) { _ReformUri(ref uri); - return Run(async (token, progress) => - { + return Run(async (token, progress) => { var request = GetAsync(uri); token.Register(request.Cancel); request.Progress = (t, p) => progress.Report(p); @@ -148,11 +128,9 @@ public IAsyncOperationWithProgress GetInputStreamAsy }); } - public IAsyncOperationWithProgress GetStringAsync(Uri uri) - { + public IAsyncOperationWithProgress GetStringAsync(Uri uri) { _ReformUri(ref uri); - return Run(async (token, progress) => - { + return Run(async (token, progress) => { var request = GetAsync(uri); token.Register(request.Cancel); request.Progress = (t, p) => progress.Report(p); @@ -163,24 +141,23 @@ public IAsyncOperationWithProgress GetStringAsync(Uri uri) }); } - public IAsyncOperationWithProgress GetDocumentAsync(Uri uri) - { + public IAsyncOperationWithProgress GetDocumentAsync(Uri uri) { _ReformUri(ref uri); - return Run(async (token, progress) => - { + return Run(async (token, progress) => { var request = GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, false); token.Register(request.Cancel); request.Progress = (t, p) => progress.Report(p); var doc = new HtmlDocument(); var response = await request; _CheckSadPanda(response); - var resstr = await response.Content.ReadAsStringAsync(); - _CheckStringResponse(resstr); - doc.LoadHtml(resstr); + var resStream = await response.Content.ReadAsInputStreamAsync(); + using var reader = new StreamReader(resStream.AsStreamForRead(), Encoding.UTF8); + var resStr = reader.ReadToEnd(); + _CheckStringResponse(resStr); + doc.LoadHtml(resStr); var rootNode = doc.DocumentNode; - do - { + do { if (response.StatusCode != HttpStatusCode.NotFound) break; @@ -195,25 +172,23 @@ public IAsyncOperationWithProgress GetDocumentAsync( break; var msg = error.GetInnerText(); - switch (msg) - { - case "This gallery has been removed or is unavailable.": - throw new InvalidOperationException(LocalizedStrings.Resources.GalleryRemoved); - case "This gallery has been locked for review. Please check back later.": - throw new InvalidOperationException(LocalizedStrings.Resources.GalleryReviewing); - default: - throw new InvalidOperationException(msg); + switch (msg) { + case "This gallery has been removed or is unavailable.": + throw new InvalidOperationException(LocalizedStrings.Resources.GalleryRemoved); + case "This gallery has been locked for review. Please check back later.": + throw new InvalidOperationException(LocalizedStrings.Resources.GalleryReviewing); + default: + throw new InvalidOperationException(msg); } } while (false); response.EnsureSuccessStatusCode(); HentaiVerseInfo.AnalyzePage(doc); - ApiToken.Update(resstr); + ApiToken.Update(resStr); return doc; }); } - public IHttpAsyncOperation PostAsync(Uri uri, IHttpContent content) - { + public IHttpAsyncOperation PostAsync(Uri uri, IHttpContent content) { _ReformUri(ref uri); return _Inner.PostAsync(uri, content); } @@ -221,17 +196,14 @@ public IHttpAsyncOperation PostAsync(Uri uri, IHttpContent content) public IHttpAsyncOperation PostAsync(Uri uri, params KeyValuePair[] content) => PostAsync(uri, (IEnumerable>)content); - public IHttpAsyncOperation PostAsync(Uri uri, IEnumerable> content) - { + public IHttpAsyncOperation PostAsync(Uri uri, IEnumerable> content) { _ReformUri(ref uri); return _Inner.PostAsync(uri, new HttpFormUrlEncodedContent(content)); } - public IAsyncOperationWithProgress PostStringAsync(Uri uri, IHttpContent content) - { + public IAsyncOperationWithProgress PostStringAsync(Uri uri, IHttpContent content) { _ReformUri(ref uri); - return Run(async (token, progress) => - { + return Run(async (token, progress) => { var op = PostAsync(uri, content); token.Register(op.Cancel); op.Progress = (sender, value) => progress.Report(value); @@ -243,8 +215,7 @@ public IAsyncOperationWithProgress PostStringAsync(Uri uri }); } - public void Dispose() - { + public void Dispose() { _Inner.Dispose(); _Nocookie.Dispose(); }