public static Task <bool> ReadAsync([NotNull] this WebRequest thisValue, [NotNull] IOResponseSettings settings, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();
            return(GetResponseAsync(thisValue, settings, token)
                   .ContinueWith(task =>
            {
                WebResponse response = task.Result;
                if (response == null)
                {
                    return false;
                }

                try
                {
                    if (token.IsCancellationRequested)
                    {
                        return false;
                    }
                    return settings.OnResponseReceived?.Invoke(response) != false && response.ReadAsync(settings, token).Execute();
                }
                catch (Exception ex) when(settings.OnError != null)
                {
                    settings.OnError(ex);
                    return false;
                }
                finally
                {
                    ObjectHelper.Dispose(ref response);
                    thisValue.Abort();
                }
            }, token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default));
        }
        public static string GetTitle([NotNull] this WebRequest thisValue, IOResponseSettings settings = null)
        {
            WebResponse response = GetResponse(thisValue, settings);

            if (response == null)
            {
                return(null);
            }

            try
            {
                return(settings?.OnResponseReceived?.Invoke(response) == false
                                                        ? null
                                                        : response.GetTitle(settings));
            }
            catch (Exception ex) when(settings?.OnError != null)
            {
                settings.OnError(ex);
                return(null);
            }
            finally
            {
                ObjectHelper.Dispose(ref response);
                thisValue.Abort();
            }
        }
        public static WebResponse GetResponse([NotNull] this WebRequest thisValue, IOResponseSettings settings = null)
        {
            if (settings == null)
            {
                return(thisValue.GetResponse());
            }

            WebResponse response          = null;
            int         retries           = 0;
            int         maxRetries        = settings.Retries;
            int         timeBeforeRetries = settings.TimeBeforeRetries.TotalIntMilliseconds();

            while (response == null && retries < maxRetries)
            {
                if (retries > 0 && timeBeforeRetries > 0)
                {
                    TimeSpanHelper.WasteTime(timeBeforeRetries);
                }

                try
                {
                    response = thisValue.GetResponse();
                }
                catch (Exception ex)
                {
                    retries++;
                    if (retries >= maxRetries)
                    {
                        settings.OnError?.Invoke(ex);
                    }
                }
            }

            return(response);
        }
        public static Stream GetStream([NotNull] this WebRequest thisValue, IOResponseSettings settings = null)
        {
            WebResponse response = GetResponse(thisValue, settings);

            if (response == null)
            {
                return(null);
            }

            try
            {
                return(settings?.OnResponseReceived?.Invoke(response) == false ? null : response.GetStream());
            }
            catch (Exception ex) when(settings?.OnError != null)
            {
                settings.OnError(ex);
                return(null);
            }
        }
        public static bool Read([NotNull] this WebRequest thisValue, [NotNull] IOResponseSettings settings)
        {
            WebResponse response = GetResponse(thisValue, settings);

            if (response == null)
            {
                return(false);
            }

            try
            {
                return(settings.OnResponseReceived?.Invoke(response) != false && response.Read(settings));
            }
            catch (Exception ex) when(settings.OnError != null)
            {
                settings.OnError(ex);
                return(false);
            }
            finally
            {
                ObjectHelper.Dispose(ref response);
                thisValue.Abort();
            }
        }
        public static Task <WebResponse> GetResponseAsync([NotNull] this WebRequest thisValue, IOResponseSettings settings = null, CancellationToken token = default(CancellationToken))
        {
            if (token.IsCancellationRequested)
            {
                return(null);
            }
            if (settings == null)
            {
                return(thisValue.GetResponseAsync());
            }

            WebResponse response          = null;
            int         retries           = 0;
            int         maxRetries        = settings.Retries;
            int         timeBeforeRetries = settings.TimeBeforeRetries.TotalIntMilliseconds();

            while (!token.IsCancellationRequested && response == null && retries < maxRetries)
            {
                if (retries > 0 && timeBeforeRetries > 0)
                {
                    TimeSpanHelper.WasteTime(timeBeforeRetries, token);
                }

                try
                {
                    response = thisValue.GetResponseAsync().Execute();
                }
                catch (Exception ex)
                {
                    retries++;
                    if (retries >= maxRetries)
                    {
                        settings.OnError?.Invoke(ex);
                    }
                }
            }

            return(Task.FromResult(response));
        }
        public static Task <Stream> GetStreamAsync([NotNull] this WebRequest thisValue, IOResponseSettings settings = null, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();
            return(GetResponseAsync(thisValue, settings, token)
                   .ContinueWith(task =>
            {
                WebResponse response = task.Result;
                if (response == null)
                {
                    return null;
                }

                try
                {
                    if (token.IsCancellationRequested)
                    {
                        return null;
                    }
                    return settings?.OnResponseReceived?.Invoke(response) == false
                                                                        ? null
                                                                        : response.GetStream();
                }
                catch (Exception ex) when(settings?.OnError != null)
                {
                    settings.OnError(ex);
                    return null;
                }
            }, token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default));
        }
        public static Task <UrlSearchResult> SearchAsync([NotNull] this WebRequest thisValue, string searchFor, UrlSearchFlags flags, IOResponseSettings settings = null, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();
            return(GetResponseAsync(thisValue, settings, token)
                   .ContinueWith(task =>
            {
                WebResponse response = task.Result;

                if (response == null)
                {
                    return null;
                }

                try
                {
                    if (token.IsCancellationRequested)
                    {
                        return null;
                    }

                    if (settings?.OnResponseReceived != null)
                    {
                        if (!settings.OnResponseReceived(response))
                        {
                            return new UrlSearchResult {
                                Status = UrlSearchStatus.Failed
                            }
                        }
                        ;
                    }

                    UrlSearchResult result = response.SearchAsync(searchFor, flags, settings, token).Execute();
                    token.ThrowIfCancellationRequested();
                    result.RedirectUri = thisValue.RequestUri != response.ResponseUri
                                                                                                ? response.ResponseUri.IsAbsoluteUri || !thisValue.RequestUri.IsAbsoluteUri
                                                                                                        ? response.ResponseUri
                                                                                                        : new Uri(thisValue.RequestUri, response.ResponseUri)
                                                                                                : thisValue.RequestUri;
                    return result;
                }
                catch (Exception ex)
                {
                    settings?.OnError?.Invoke(ex);
                    return new UrlSearchResult
                    {
                        Status = UrlSearchStatus.Error,
                        Exception = ex
                    };
                }
                finally
                {
                    ObjectHelper.Dispose(ref response);
                    thisValue.Abort();
                }
            }, token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default));
        }
 public static Task <UrlSearchResult> SearchAsync([NotNull] this WebRequest thisValue, UrlSearchFlags flags, IOResponseSettings settings = null, CancellationToken token = default(CancellationToken))
 {
     return(SearchAsync(thisValue, null, flags, settings, token));
 }
Esempio n. 10
0
        public static UrlSearchResult Search([NotNull] this WebRequest thisValue, string searchFor, UrlSearchFlags flags, IOResponseSettings settings = null)
        {
            WebResponse response = GetResponse(thisValue, settings);

            if (response == null)
            {
                return new UrlSearchResult {
                           Status = UrlSearchStatus.Failed
                }
            }
            ;

            try
            {
                if (settings?.OnResponseReceived != null)
                {
                    if (!settings.OnResponseReceived(response))
                    {
                        return new UrlSearchResult {
                                   Status = UrlSearchStatus.Failed
                        }
                    }
                    ;
                }

                UrlSearchResult result = response.Search(searchFor, flags, settings);

                result.RedirectUri = thisValue.RequestUri != response.ResponseUri
                                        ? response.ResponseUri.IsAbsoluteUri || !thisValue.RequestUri.IsAbsoluteUri
                                                ? response.ResponseUri
                                                : new Uri(thisValue.RequestUri, response.ResponseUri)
                                        : thisValue.RequestUri;
                return(result);
            }
            catch (Exception ex)
            {
                settings?.OnError?.Invoke(ex);
                return(new UrlSearchResult
                {
                    Status = UrlSearchStatus.Error,
                    Exception = ex
                });
            }
            finally
            {
                ObjectHelper.Dispose(ref response);
                thisValue.Abort();
            }
        }
Esempio n. 11
0
 public static UrlSearchResult Search([NotNull] this WebRequest thisValue, UrlSearchFlags flags, IOResponseSettings settings = null)
 {
     return(Search(thisValue, null, flags, settings));
 }
Esempio n. 12
0
        public static Task <(string Title, string Buffer)> PeekAsync([NotNull] this WebRequest thisValue, IOResponseSettings settings = null, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();
            return(GetResponseAsync(thisValue, settings, token)
                   .ContinueWith(task =>
            {
                WebResponse response = task.Result;
                if (response == null)
                {
                    return (null, null);
                }

                try
                {
                    return token.IsCancellationRequested
                                                                        ? (null, null)
                                                                        : settings?.OnResponseReceived?.Invoke(response) == false
                                                                                ? (null, null)
                                                                                : response.PeekAsync(settings, token).Execute();
                }
                catch (Exception ex) when(settings?.OnError != null)
                {
                    settings.OnError(ex);
                    return (null, null);
                }
                finally
                {
                    ObjectHelper.Dispose(ref response);
                    thisValue.Abort();
                }
            }, token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default));
        }