From 88cea5d6f4f9f3d25c84901833b0a610f9e622e3 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Wed, 28 Nov 2018 02:30:50 +0100 Subject: [PATCH] Misc WebBrowser improvements Just when you thought that you've seen everything, wait for 200 OK with JSON cut in half. --- ArchiSteamFarm/WebBrowser.cs | 112 ++++++++++++++++++++++------------- 1 file changed, 71 insertions(+), 41 deletions(-) diff --git a/ArchiSteamFarm/WebBrowser.cs b/ArchiSteamFarm/WebBrowser.cs index ed5bafa68..8882f8867 100644 --- a/ArchiSteamFarm/WebBrowser.cs +++ b/ArchiSteamFarm/WebBrowser.cs @@ -181,26 +181,36 @@ namespace ArchiSteamFarm { return null; } - StringResponse response = await UrlGetToString(request, referer, maxTries).ConfigureAwait(false); - if (string.IsNullOrEmpty(response?.Content)) { - return null; - } + for (byte i = 0; i < maxTries; i++) { + StringResponse response = await UrlGetToString(request, referer, 1).ConfigureAwait(false); - T obj; - - try { - obj = JsonConvert.DeserializeObject(response.Content); - } catch (JsonException e) { - ArchiLogger.LogGenericException(e); - - if (Debugging.IsUserDebugging) { - ArchiLogger.LogGenericDebug(string.Format(Strings.Content, response.Content)); + if (string.IsNullOrEmpty(response?.Content)) { + continue; } - return null; + T obj; + + try { + obj = JsonConvert.DeserializeObject(response.Content); + } catch (JsonException e) { + ArchiLogger.LogGenericWarningException(e); + + if (Debugging.IsUserDebugging) { + ArchiLogger.LogGenericDebug(string.Format(Strings.Content, response.Content)); + } + + continue; + } + + return new ObjectResponse(response, obj); } - return new ObjectResponse(response, obj); + if (maxTries > 1) { + ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries)); + ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); + } + + return null; } internal async Task UrlGetToString(string request, string referer = null, byte maxTries = MaxTries) { @@ -233,21 +243,31 @@ namespace ArchiSteamFarm { return null; } - StringResponse response = await UrlGetToString(request, referer, maxTries).ConfigureAwait(false); - if (string.IsNullOrEmpty(response?.Content)) { - return null; + for (byte i = 0; i < maxTries; i++) { + StringResponse response = await UrlGetToString(request, referer, 1).ConfigureAwait(false); + + if (string.IsNullOrEmpty(response?.Content)) { + continue; + } + + XmlDocument xmlDocument = new XmlDocument(); + + try { + xmlDocument.LoadXml(response.Content); + } catch (XmlException e) { + ArchiLogger.LogGenericWarningException(e); + continue; + } + + return new XmlDocumentResponse(response, xmlDocument); } - XmlDocument xmlDocument = new XmlDocument(); - - try { - xmlDocument.LoadXml(response.Content); - } catch (XmlException e) { - ArchiLogger.LogGenericException(e); - return null; + if (maxTries > 1) { + ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries)); + ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); } - return new XmlDocumentResponse(response, xmlDocument); + return null; } internal async Task UrlHead(string request, string referer = null, byte maxTries = MaxTries) { @@ -314,26 +334,36 @@ namespace ArchiSteamFarm { return null; } - StringResponse response = await UrlPostToString(request, data, referer, maxTries).ConfigureAwait(false); - if (string.IsNullOrEmpty(response?.Content)) { - return null; - } + for (byte i = 0; i < maxTries; i++) { + StringResponse response = await UrlPostToString(request, data, referer, maxTries).ConfigureAwait(false); - T obj; - - try { - obj = JsonConvert.DeserializeObject(response.Content); - } catch (JsonException e) { - ArchiLogger.LogGenericException(e); - - if (Debugging.IsUserDebugging) { - ArchiLogger.LogGenericDebug(string.Format(Strings.Content, response.Content)); + if (string.IsNullOrEmpty(response?.Content)) { + continue; } - return null; + T obj; + + try { + obj = JsonConvert.DeserializeObject(response.Content); + } catch (JsonException e) { + ArchiLogger.LogGenericWarningException(e); + + if (Debugging.IsUserDebugging) { + ArchiLogger.LogGenericDebug(string.Format(Strings.Content, response.Content)); + } + + continue; + } + + return new ObjectResponse(response, obj); } - return new ObjectResponse(response, obj); + if (maxTries > 1) { + ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries)); + ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); + } + + return null; } private async Task InternalGet(string request, string referer = null, HttpCompletionOption httpCompletionOptions = HttpCompletionOption.ResponseContentRead) {