Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
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();
            }
        }