Exemplo n.º 1
0
        private async Task <string> GeStringAsyncCore(Task <HttpResponseMessage> getTask)
        {
            // Wait for the response message.
            using (HttpResponseMessage responseMessage = await getTask.ConfigureAwait(false))
            {
                // Make sure it completed successfully.
                responseMessage.EnsureSuccessStatusCode();

                // Get the response content.
                HttpContent c = responseMessage.Content;
                if (c != null)
                {
                    HttpContentHeaders headers = c.Headers;

                    // Since the underlying byte[] will never be exposed, we use an ArrayPool-backed
                    // stream to which we copy all of the data from the response.
                    using (Stream responseStream = await c.ReadAsStreamAsync().ConfigureAwait(false))
                        using (var buffer = new HttpContent.LimitArrayPoolWriteStream(_maxResponseContentBufferSize, (int)headers.ContentLength.GetValueOrDefault()))
                        {
                            await responseStream.CopyToAsync(buffer).ConfigureAwait(false);

                            if (buffer.Length > 0)
                            {
                                // Decode and return the data from the buffer.
                                return(HttpContent.ReadBufferAsString(buffer.GetBuffer(), headers));
                            }
                        }
                }

                // No content to return.
                return(string.Empty);
            }
        }
Exemplo n.º 2
0
        private string ReadBufferedContentAsString()
        {
            if (this._bufferedContent.Length == 0L)
            {
                return(string.Empty);
            }
            ArraySegment <byte> buffer;

            if (!this.TryGetBuffer(out buffer))
            {
                buffer = new ArraySegment <byte>(this._bufferedContent.ToArray());
            }
            return(HttpContent.ReadBufferAsString(buffer, this.Headers));
        }
Exemplo n.º 3
0
 private async Task <string> GetStringAsyncCore(Task <HttpResponseMessage> getTask)
 {
     using (HttpResponseMessage responseMessage = await getTask.ConfigureAwait(false))
     {
         responseMessage.EnsureSuccessStatusCode();
         HttpContent content = responseMessage.Content;
         if (content != null)
         {
             HttpContentHeaders headers = content.Headers;
             Stream             stream  = content.TryReadAsStream();
             if (stream == null)
             {
                 stream = await content.ReadAsStreamAsync().ConfigureAwait(false);
             }
             using (Stream responseStream = stream)
             {
                 using (HttpContent.LimitArrayPoolWriteStream buffer = new HttpContent.LimitArrayPoolWriteStream(this._maxResponseContentBufferSize, (long)(int)headers.ContentLength.GetValueOrDefault()))
                 {
                     try
                     {
                         await responseStream.CopyToAsync((Stream)buffer).ConfigureAwait(false);
                     }
                     catch (Exception ex) when(HttpContent.StreamCopyExceptionNeedsWrapping(ex))
                     {
                         throw HttpContent.WrapStreamCopyException(ex);
                     }
                     if (buffer.Length > 0L)
                     {
                         return(HttpContent.ReadBufferAsString(buffer.GetBuffer(), headers));
                     }
                 }
             }
             headers = (HttpContentHeaders)null;
         }
         return(string.Empty);
     }
 }