public async Task <XmlDocumentResponse?> UrlGetToXmlDocument(string request, string?referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries) { if (string.IsNullOrEmpty(request) || (maxTries == 0)) { throw new ArgumentNullException(nameof(request) + " || " + nameof(maxTries)); } XmlDocumentResponse?result = null; for (byte i = 0; i < maxTries; i++) { await using StreamResponse? response = await UrlGetToStream(request, referer, requestOptions | ERequestOptions.ReturnClientErrors, 1).ConfigureAwait(false); if (response?.StatusCode.IsClientErrorCode() == true) { if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) { result = new XmlDocumentResponse(response); } break; } if (response?.StatusCode.IsServerErrorCode() == true) { if (requestOptions.HasFlag(ERequestOptions.ReturnServerErrors)) { result = new XmlDocumentResponse(response); } break; } if (response?.Content == null) { continue; } XmlDocument xmlDocument = new XmlDocument(); try { xmlDocument.Load(response.Content); } catch (XmlException e) { ArchiLogger.LogGenericWarningException(e); continue; } return(new XmlDocumentResponse(response, xmlDocument)); } if (maxTries > 1) { ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries)); ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); } return(result); }
private async Task <StreamResponse?> UrlPostToStream <T>(string request, T?data = null, string?referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries) where T : class { if (string.IsNullOrEmpty(request) || (maxTries == 0)) { throw new ArgumentNullException(nameof(request) + " || " + nameof(maxTries)); } StreamResponse?result = null; for (byte i = 0; i < maxTries; i++) { HttpResponseMessage?response = await InternalPost(request, data, referer, requestOptions, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false); if (response?.StatusCode.IsClientErrorCode() == true) { if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) { result = new StreamResponse(response); } break; } if (response?.StatusCode.IsServerErrorCode() == true) { if (requestOptions.HasFlag(ERequestOptions.ReturnServerErrors)) { result = new StreamResponse(response); } break; } if (response?.Content == null) { continue; } return(new StreamResponse(response, await response.Content.ReadAsStreamAsync().ConfigureAwait(false))); } if (maxTries > 1) { ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries)); ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); } return(result); }
internal async Task <BinaryResponse?> UrlGetToBinary(string request, string?referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries, IProgress <byte>?progressReporter = null) { if (string.IsNullOrEmpty(request) || (maxTries == 0)) { throw new ArgumentNullException(nameof(request) + " || " + nameof(maxTries)); } BinaryResponse?result = null; for (byte i = 0; i < maxTries; i++) { await using StreamResponse? response = await UrlGetToStream(request, referer, requestOptions | ERequestOptions.ReturnClientErrors, 1).ConfigureAwait(false); if (response?.StatusCode.IsClientErrorCode() == true) { if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) { result = new BinaryResponse(response); } break; } if (response?.StatusCode.IsServerErrorCode() == true) { if (requestOptions.HasFlag(ERequestOptions.ReturnServerErrors)) { result = new BinaryResponse(response); } break; } if (response?.Content == null) { continue; } progressReporter?.Report(0); #if NETFRAMEWORK using MemoryStream ms = new MemoryStream((int)response.Length); #else await using MemoryStream ms = new MemoryStream((int)response.Length); #endif try { byte batch = 0; uint readThisBatch = 0; uint batchIncreaseSize = response.Length / 100; byte[] buffer = new byte[8192]; // This is HttpClient's buffer, using more doesn't make sense while (response.Content.CanRead) { int read = await response.Content.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); if (read == 0) { break; } await ms.WriteAsync(buffer, 0, read).ConfigureAwait(false); if ((batchIncreaseSize == 0) || (batch >= 99)) { continue; } readThisBatch += (uint)read; if (readThisBatch < batchIncreaseSize) { continue; } readThisBatch -= batchIncreaseSize; progressReporter?.Report(++batch); } } catch (Exception e) { ArchiLogger.LogGenericDebuggingException(e); return(null); } progressReporter?.Report(100); return(new BinaryResponse(response, ms.ToArray())); } if (maxTries > 1) { ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries)); ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); } return(result); }
public async Task <ObjectResponse <TResult>?> UrlPostToJsonObject <TResult, TData>(string request, TData?data = null, string?referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries) where TResult : class where TData : class { if (string.IsNullOrEmpty(request) || (maxTries == 0)) { throw new ArgumentNullException(nameof(request) + " || " + nameof(maxTries)); } ObjectResponse <TResult>?result = null; for (byte i = 0; i < maxTries; i++) { await using StreamResponse? response = await UrlPostToStream(request, data, referer, requestOptions | ERequestOptions.ReturnClientErrors, 1).ConfigureAwait(false); if (response?.StatusCode.IsClientErrorCode() == true) { if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) { result = new ObjectResponse <TResult>(response); } break; } if (response?.StatusCode.IsServerErrorCode() == true) { if (requestOptions.HasFlag(ERequestOptions.ReturnServerErrors)) { result = new ObjectResponse <TResult>(response); } break; } if (response?.Content == null) { continue; } TResult?obj; try { using StreamReader steamReader = new StreamReader(response.Content); using JsonReader jsonReader = new JsonTextReader(steamReader); JsonSerializer serializer = new JsonSerializer(); obj = serializer.Deserialize <TResult>(jsonReader); if (obj == null) { ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorIsEmpty, nameof(obj))); continue; } } catch (Exception e) { ArchiLogger.LogGenericWarningException(e); continue; } return(new ObjectResponse <TResult>(response, obj)); } if (maxTries > 1) { ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries)); ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); } return(result); }