public async Task <ObjectResponse <T> > UrlPostToJsonObject <T>(string request, IReadOnlyCollection <KeyValuePair <string, string> > data = null, string referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries) where T : class { if (string.IsNullOrEmpty(request) || (maxTries == 0)) { ArchiLogger.LogNullError(nameof(request) + " || " + nameof(maxTries)); return(null); } ObjectResponse <T> 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 == null) { return(null); } if (response.StatusCode.IsClientErrorCode()) { if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) { result = new ObjectResponse <T>(response); } break; } if (response.Content == null) { continue; } T obj; try { using StreamReader steamReader = new StreamReader(response.Content); using JsonReader jsonReader = new JsonTextReader(steamReader); JsonSerializer serializer = new JsonSerializer(); obj = serializer.Deserialize <T>(jsonReader); } catch (Exception e) { ArchiLogger.LogGenericWarningException(e); continue; } return(new ObjectResponse <T>(response, obj)); } if (maxTries > 1) { ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries)); ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); } return(result); }
internal XmlDocumentResponse(StreamResponse streamResponse, XmlDocument content) : this(streamResponse) { if (streamResponse == null) { throw new ArgumentNullException(nameof(streamResponse)); } Content = content; }
private HtmlDocumentResponse(StreamResponse streamResponse, IDocument document) : this(streamResponse) { if ((streamResponse == null) || (document == null)) { throw new ArgumentNullException(nameof(streamResponse) + " || " + nameof(document)); } Content = document; }
internal ObjectResponse([NotNull] StreamResponse streamResponse, T content) : this(streamResponse) { if (streamResponse == null) { throw new ArgumentNullException(nameof(streamResponse)); } Content = content; }
public async Task <XmlDocumentResponse> UrlGetToXmlDocument(string request, string referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries) { if (string.IsNullOrEmpty(request) || (maxTries == 0)) { ArchiLogger.LogNullError(nameof(request) + " || " + nameof(maxTries)); return(null); } XmlDocumentResponse result = null; for (byte i = 0; i < maxTries; i++) { using StreamResponse response = await UrlGetToStream(request, referer, requestOptions | ERequestOptions.ReturnClientErrors, 1).ConfigureAwait(false); // ReSharper disable once UseNullPropagationWhenPossible - false check if (response == null) { continue; } if (response.StatusCode.IsClientErrorCode()) { if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) { 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); }
public async Task <HtmlDocumentResponse> UrlPostToHtmlDocument(string request, IReadOnlyCollection <KeyValuePair <string, string> > data = null, string referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries) { if (string.IsNullOrEmpty(request) || (maxTries == 0)) { ArchiLogger.LogNullError(nameof(request) + " || " + nameof(maxTries)); return(null); } HtmlDocumentResponse result = null; for (int i = 0; i < maxTries; i++) { await using StreamResponse response = await UrlPostToStream(request, data, referer, requestOptions | ERequestOptions.ReturnClientErrors, 1).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); }
internal static async Task <HtmlDocumentResponse> 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)); }
public async Task <HtmlDocumentResponse> UrlPostToHtmlDocument(string request, IReadOnlyCollection <KeyValuePair <string, string> > data = null, string referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries) { if (string.IsNullOrEmpty(request) || (maxTries == 0)) { ArchiLogger.LogNullError(nameof(request) + " || " + nameof(maxTries)); return(null); } using StreamResponse response = await UrlPostToStream(request, data, referer, requestOptions, maxTries).ConfigureAwait(false); return(response != null ? await HtmlDocumentResponse.Create(response).ConfigureAwait(false) : null); }
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 static async Task <HtmlDocumentResponse?> Create(StreamResponse streamResponse) { if (streamResponse == null) { throw new ArgumentNullException(nameof(streamResponse)); } IBrowsingContext context = BrowsingContext.New(); 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); } }
private async Task <StreamResponse> UrlPostToStream(string request, IReadOnlyCollection <KeyValuePair <string, string> > data = null, string referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries) { if (string.IsNullOrEmpty(request) || (maxTries == 0)) { ArchiLogger.LogNullError(nameof(request) + " || " + nameof(maxTries)); return(null); } StreamResponse result = null; for (byte i = 0; i < maxTries; i++) { HttpResponseMessage response = await InternalPost(request, data, referer, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false); if (response == null) { continue; } if (response.StatusCode.IsClientErrorCode()) { if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) { result = new StreamResponse(response); } break; } 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> UrlGetToBinaryWithProgress(string request, string referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries) { if (string.IsNullOrEmpty(request) || (maxTries == 0)) { ArchiLogger.LogNullError(nameof(request) + " || " + nameof(maxTries)); return(null); } BinaryResponse result = null; for (byte i = 0; i < maxTries; i++) { const byte printPercentage = 10; const byte maxBatches = 99 / printPercentage; using StreamResponse response = await UrlGetToStream(request, referer, requestOptions).ConfigureAwait(false); if (response == null) { continue; } if (response.StatusCode.IsClientErrorCode()) { if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) { result = new BinaryResponse(response); } break; } ArchiLogger.LogGenericDebug("0%..."); using MemoryStream ms = new MemoryStream((int)response.Length); try { using Stream contentStream = response.Content; byte batch = 0; uint readThisBatch = 0; byte[] buffer = new byte[8192]; // This is HttpClient's buffer, using more doesn't make sense while (contentStream.CanRead) { int read = await contentStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); if (read == 0) { break; } await ms.WriteAsync(buffer, 0, read).ConfigureAwait(false); if ((response.Length == 0) || (batch >= maxBatches)) { continue; } readThisBatch += (uint)read; if (readThisBatch < response.Length / printPercentage) { continue; } readThisBatch -= response.Length / printPercentage; ArchiLogger.LogGenericDebug((++batch * printPercentage) + "%..."); } } catch (Exception e) { ArchiLogger.LogGenericDebuggingException(e); return(null); } ArchiLogger.LogGenericDebug("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 <T> > UrlGetToJsonObject <T>(string request, string referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries) where T : class { if (string.IsNullOrEmpty(request) || (maxTries == 0)) { ArchiLogger.LogNullError(nameof(request) + " || " + nameof(maxTries)); return(null); } ObjectResponse <T> result = null; for (byte i = 0; i < maxTries; i++) { using StreamResponse response = await UrlGetToStream(request, referer, requestOptions | ERequestOptions.ReturnClientErrors, 1).ConfigureAwait(false); // ReSharper disable once UseNullPropagationWhenPossible - false check if (response == null) { continue; } if (response.StatusCode.IsClientErrorCode()) { if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) { result = new ObjectResponse <T>(response); } break; } if (response.Content == null) { continue; } T obj; try { using StreamReader streamReader = new StreamReader(response.Content); using JsonReader jsonReader = new JsonTextReader(streamReader); JsonSerializer serializer = new JsonSerializer(); obj = serializer.Deserialize <T>(jsonReader); } catch (JsonException e) { ArchiLogger.LogGenericWarningException(e); if (Debugging.IsUserDebugging) { ArchiLogger.LogGenericDebug(string.Format(Strings.Content, response.Content)); } continue; } return(new ObjectResponse <T>(response, obj)); } if (maxTries > 1) { ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries)); ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); } return(result); }