diff --git a/ArchiSteamFarm/WebBrowser.cs b/ArchiSteamFarm/WebBrowser.cs index d422f2684..356da913e 100644 --- a/ArchiSteamFarm/WebBrowser.cs +++ b/ArchiSteamFarm/WebBrowser.cs @@ -105,9 +105,44 @@ namespace ArchiSteamFarm { return null; } - using StreamResponse response = await UrlGetToStream(request, referer, requestOptions, maxTries).ConfigureAwait(false); + HtmlDocumentResponse result = null; - return response != null ? await HtmlDocumentResponse.Create(response).ConfigureAwait(false) : null; + for (int i = 0; i < maxTries; i++) { + using StreamResponse response = await UrlGetToStream(request, referer, requestOptions, maxTries).ConfigureAwait(false); + + if (response == null) { + return null; + } + + if (response.StatusCode.IsClientErrorCode()) { + if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) { + result = new HtmlDocumentResponse(response); + } + + break; + } + + if (response.Content == null) { + continue; + } + + try { + result = await HtmlDocumentResponse.Create(response).ConfigureAwait(false); + } catch (Exception e) { + ArchiLogger.LogGenericWarningException(e); + + continue; + } + + return result; + } + + if (maxTries > 1) { + ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries)); + ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); + } + + return result; } [ItemCanBeNull] @@ -149,13 +184,9 @@ namespace ArchiSteamFarm { JsonSerializer serializer = new JsonSerializer(); obj = serializer.Deserialize(jsonReader); - } catch (JsonException e) { + } catch (Exception e) { ArchiLogger.LogGenericWarningException(e); - if (Debugging.IsUserDebugging) { - ArchiLogger.LogGenericDebug(string.Format(Strings.Content, response.Content)); - } - continue; } @@ -305,9 +336,44 @@ namespace ArchiSteamFarm { return null; } - using StreamResponse response = await UrlPostToStream(request, data, referer, requestOptions, maxTries).ConfigureAwait(false); + HtmlDocumentResponse result = null; - return response != null ? await HtmlDocumentResponse.Create(response).ConfigureAwait(false) : null; + for (int i = 0; i < maxTries; i++) { + using StreamResponse response = await UrlPostToStream(request, data, referer, requestOptions, maxTries).ConfigureAwait(false); + + if (response == null) { + return null; + } + + if (response.StatusCode.IsClientErrorCode()) { + if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) { + result = new HtmlDocumentResponse(response); + } + + break; + } + + if (response.Content == null) { + continue; + } + + try { + result = await HtmlDocumentResponse.Create(response).ConfigureAwait(false); + } catch (Exception e) { + ArchiLogger.LogGenericWarningException(e); + + continue; + } + + return result; + } + + if (maxTries > 1) { + ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries)); + ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); + } + + return result; } [ItemCanBeNull] @@ -348,13 +414,9 @@ namespace ArchiSteamFarm { JsonSerializer serializer = new JsonSerializer(); obj = serializer.Deserialize(jsonReader); - } catch (JsonException e) { + } catch (Exception e) { ArchiLogger.LogGenericWarningException(e); - if (Debugging.IsUserDebugging) { - ArchiLogger.LogGenericDebug(string.Format(Strings.Content, response.Content)); - } - continue; } @@ -749,18 +811,33 @@ namespace ArchiSteamFarm { [PublicAPI] public readonly IDocument Content; - private HtmlDocumentResponse(BasicResponse streamResponse, IDocument document) : base(streamResponse) => Content = document; + internal HtmlDocumentResponse([NotNull] BasicResponse basicResponse) : base(basicResponse) { } - [ItemNotNull] + private HtmlDocumentResponse([NotNull] StreamResponse streamResponse, [NotNull] IDocument document) : base(streamResponse) { + if ((streamResponse == null) || (document == null)) { + throw new ArgumentNullException(nameof(streamResponse) + " || " + nameof(document)); + } + + Content = document; + } + + [ItemCanBeNull] internal static async Task Create([NotNull] StreamResponse streamResponse) { if (streamResponse == null) { throw new ArgumentNullException(nameof(streamResponse)); } IBrowsingContext context = BrowsingContext.New(Configuration.Default.WithXPath()); - IDocument document = await context.OpenAsync(req => req.Content(streamResponse.Content, true)).ConfigureAwait(false); - return new HtmlDocumentResponse(streamResponse, document); + try { + IDocument document = await context.OpenAsync(req => req.Content(streamResponse.Content, true)).ConfigureAwait(false); + + return new HtmlDocumentResponse(streamResponse, document); + } catch (Exception e) { + ASF.ArchiLogger.LogGenericWarningException(e); + + return null; + } } }