Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
0
        public static (string Title, string Buffer) Peek([NotNull] this WebRequest thisValue, IOResponseSettings settings = null)
        {
            WebResponse response = GetResponse(thisValue, settings);

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

            try
            {
                return(settings?.OnResponseReceived?.Invoke(response) == false
                                                        ? (null, null)
                                                        : response.Peek(settings));
            }
            catch (Exception ex) when(settings?.OnError != null)
            {
                settings.OnError(ex);
                return(null, null);
            }
            finally
            {
                ObjectHelper.Dispose(ref response);
                thisValue.Abort();
            }
        }
Exemplo n.º 5
0
        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);
            }
        }
Exemplo n.º 6
0
        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();
            }
        }