コード例 #1
0
        public static Task <string> ReadToEndAsync([NotNull] this WebResponse thisValue, IOSettings settings = null, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();
            settings ??= new IOSettings();

            Stream       stream = null;
            StreamReader reader = null;
            string       result;

            try
            {
                stream = GetStream(thisValue);
                token.ThrowIfCancellationRequested();
                if (stream == null)
                {
                    return(null);
                }
                reader = new StreamReader(stream, settings.Encoding, true, settings.BufferSize);
                result = reader.ReadToEndAsync().Execute();
                token.ThrowIfCancellationRequested();
            }
            catch (Exception ex) when(settings.OnError != null)
            {
                result = null;
                settings.OnError(ex);
            }
            finally
            {
                ObjectHelper.Dispose(ref reader);
                ObjectHelper.Dispose(ref stream);
            }

            return(Task.FromResult(result));
        }
コード例 #2
0
        public static bool Read([NotNull] this WebResponse thisValue, [NotNull] IOSettings settings)
        {
            IIOOnRead    ioOnRead = settings as IIOOnRead ?? throw new ArgumentException("Argument must provide an OnRead implementation.", nameof(settings));
            Stream       stream   = null;
            StreamReader reader   = null;

            try
            {
                stream = GetStream(thisValue);
                if (stream == null)
                {
                    return(false);
                }
                reader = new StreamReader(stream, settings.Encoding, true);
                int    length;
                char[] chars = new char[settings.BufferSize];

                do
                {
                    length = reader.Read(chars);
                }while (length > 0 && ioOnRead.OnRead(chars, length));

                return(true);
            }
            catch (Exception ex) when(settings.OnError != null)
            {
                settings.OnError(ex);
                return(false);
            }
            finally
            {
                ObjectHelper.Dispose(ref reader);
                ObjectHelper.Dispose(ref stream);
            }
        }
コード例 #3
0
        public static string ReadToEnd([NotNull] this WebResponse thisValue, IOSettings settings = null)
        {
            settings ??= new IOSettings();

            Stream       stream = null;
            StreamReader reader = null;
            string       result;

            try
            {
                stream = GetStream(thisValue);
                if (stream == null)
                {
                    return(null);
                }
                reader = new StreamReader(stream, settings.Encoding, true, settings.BufferSize);
                result = reader.ReadToEnd();
            }
            catch (Exception ex) when(settings.OnError != null)
            {
                result = null;
                settings.OnError(ex);
            }
            finally
            {
                ObjectHelper.Dispose(ref reader);
                ObjectHelper.Dispose(ref stream);
            }

            return(result);
        }
コード例 #4
0
        public static Task <bool> ReadAsync([NotNull] this WebResponse thisValue, [NotNull] IOSettings settings, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();

            IIOOnRead    ioOnRead = settings as IIOOnRead ?? throw new ArgumentException("Argument must provide an OnRead implementation.", nameof(settings));
            Stream       stream   = null;
            StreamReader reader   = null;

            try
            {
                stream = GetStream(thisValue);
                token.ThrowIfCancellationRequested();
                if (stream == null)
                {
                    return(Task.FromResult(false));
                }
                reader = new StreamReader(stream, settings.Encoding, true);

                int    length;
                char[] chars = new char[settings.BufferSize];

                do
                {
                    length = reader.ReadAsync(chars).Execute();
                }while (!token.IsCancellationRequested && length > 0 && ioOnRead.OnRead(chars, length));

                token.ThrowIfCancellationRequested();
                return(Task.FromResult(true));
            }
            catch (Exception ex) when(settings.OnError != null)
            {
                settings.OnError(ex);
                return(Task.FromResult(false));
            }
            finally
            {
                ObjectHelper.Dispose(ref reader);
                ObjectHelper.Dispose(ref stream);
            }
        }