ReadAsStreamAsync() 공개 메소드

public ReadAsStreamAsync ( ) : Task
리턴 Task
예제 #1
1
 public static async Task<BerkeleyDtoResult> GetResultAsync(JsonSerializer serializer, HttpContent content)
 {
     using (Stream stream = await content.ReadAsStreamAsync().ConfigureAwait(false))
     using (var reader = new StreamReader(stream))
     using (var jsonReader = new JsonTextReader(reader))
         return serializer.Deserialize<BerkeleyDtoResult>(jsonReader);
 }
예제 #2
0
        public async Task <ActionResult> GetAttachment(int FileID)
        {
            UriBuilder uriBuilder = new UriBuilder();

            uriBuilder.Scheme = "https";
            uriBuilder.Host   = "api.example.com";

            var Path = "/files/download";

            uriBuilder.Path = Path;
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(uriBuilder.ToString());
                client.DefaultRequestHeaders.Accept.Clear();
                //client.DefaultRequestHeaders.Add("authorization", access_token); //if any
                //client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = await client.GetAsync(uriBuilder.ToString());

                if (response.IsSuccessStatusCode)
                {
                    response.Headers[""];
                    System.Net.Http.HttpContent content = response.Content;
                    var contentStream = await content.ReadAsStreamAsync();                     // get the actual content stream

                    return(File(contentStream, content_type, filename));
                }
                else
                {
                    throw new FileNotFoundException();
                }
            }
        }
예제 #3
0
        public async Task Download()
        {
            var index = 0;

            for (int i = 0; i < files.Count; i++)
            {
                if (files[i] == selectedfile)
                {
                    index = i;
                }
            }
            if (index != 0)
            {
                UploadText.Text = selectedfile + " has been downloaded to [DownloadBook] folder!";
                string path = downloadpath + "\\" + selectedfile;
                Console.Write("\n[{0} has been downloaded to {1}!]", files[index], path);
                //download file
                string display = baseUrl_ + "/" + index;
                var    resp2   = await client.GetAsync(baseUrl_ + "/" + index).ConfigureAwait(false);

                System.Net.Http.HttpContent content = resp2.Content;       // actually a System.Net.Http.StreamContent instance but you do not need to cast as the actual type does not matter in this case
                using (var file = System.IO.File.Create(path))
                {                                                          // create a new file to write to
                    var contentStream = await content.ReadAsStreamAsync(); // get the actual content stream

                    await contentStream.CopyToAsync(file);                 // copy that stream to the file stream

                    await file.FlushAsync();                               // flush back to disk before disposing
                }
            }
        }
예제 #4
0
        private static async Task GetAttachment(int FileID)
        {
            UriBuilder uriBuilder = new UriBuilder();

            uriBuilder.Scheme = "https";
            uriBuilder.Host   = "www1.nseindia.com/content/historical/EQUITIES/2020/MAR/cm04MAR2020bhav.csv.zip";
            //https://www1.nseindia.com/content/historical/EQUITIES/2020/MAR/cm04MAR2020bhav.csv.zip
            var Path = "/files/download";

            uriBuilder.Path = Path;
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(uriBuilder.ToString());
                client.DefaultRequestHeaders.Accept.Clear();
                //client.DefaultRequestHeaders.Add("authorization", access_token); //if any
                //client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = await client.GetAsync(uriBuilder.ToString());

                if (response.IsSuccessStatusCode)
                {
                    System.Net.Http.HttpContent content = response.Content;
                    var contentStream = await content.ReadAsStreamAsync(); // get the actual content stream

                    // return File(contentStream, content_type, filename);
                }
                else
                {
                    throw new FileNotFoundException();
                }
            }
        }
예제 #5
0
파일: Download.cs 프로젝트: amseet/Orion
        public static async Task GetAttachment(string url, string filename)
        {
            UriBuilder uriBuilder = new UriBuilder(url);

            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(uriBuilder.ToString());
                client.DefaultRequestHeaders.Accept.Clear();
                HttpResponseMessage response = await client.GetAsync(uriBuilder.ToString());

                if (response.IsSuccessStatusCode)
                {
                    System.Net.Http.HttpContent content = response.Content;
                    var contentStream = await content.ReadAsStreamAsync(); // get the actual content stream

                    using (var outputStream = File.OpenWrite(filename))
                    {
                        contentStream.CopyTo(outputStream);
                        contentStream.Flush();
                    }
                }
                else
                {
                    throw new FileNotFoundException();
                }
            }
        }
예제 #6
0
        public async Task DownloadFile(string route, string filename)
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            var httpClient = await GetHttpClient();

            HttpResponseMessage response = await httpClient.GetAsync(route);

            if (response.IsSuccessStatusCode)
            {
                System.Net.Http.HttpContent content = response.Content;
                var contentStream = await content.ReadAsStreamAsync();

                using (var stream = new FileStream(filename, FileMode.Create))
                {
                    await contentStream.CopyToAsync(stream);
                }
            }
            else
            {
                throw new FileNotFoundException();
            }
        }
        HttpContent IHttpContentEncryptor.Encrypt(HttpContent originContent)
        {
            if (originContent == null)
            {
                throw new ArgumentNullException("originContent");
            }

            var originData = originContent.ReadAsStreamAsync().Result;

            if (originData.Length == 0)
            {
                return originContent;
            }

            this.algorithm.Key = this.keyProvider.Key;
            this.algorithm.IV = this.keyProvider.IV;

            using (var encryptor = algorithm.CreateEncryptor())
            {
                using (var encryptedData = new MemoryStream())
                {
                    var cryptoStream = new CryptoStream(encryptedData, encryptor, CryptoStreamMode.Write);
                    originData.CopyTo(cryptoStream);
                    cryptoStream.FlushFinalBlock();
                    encryptedData.Position = 0;

                    var encodedString = Convert.ToBase64String(encryptedData.ToArray());
                    var encryptedContent = new StringContent(encodedString);
                    encryptedContent.Headers.ContentType = originContent.Headers.ContentType;

                    return encryptedContent;
                }
            }
        }
예제 #8
0
        private Location readJsonIntoAddress(HttpContent content)
        {
            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));
            Response bingResponse = jsonSerializer.ReadObject(content.ReadAsStreamAsync().Result) as Response;

            Location bingLocation = bingResponse.ResourceSets[0].Resources[0] as Location;

            return bingLocation;
        }
 public override async Task<object> ReadFromStreamAsync(Type type,
                                                        Stream readStream,
                                                        HttpContent content,
                                                        IFormatterLogger formatterLogger,
                                                        CancellationToken cancellationToken)
 {
     var uf = new UploadedFile(StrHelper.UnquoteToken(content.Headers.ContentDisposition.FileName),
                               await content.ReadAsStreamAsync());
     return uf;
 }
예제 #10
0
        public string ReadResponse(HttpResponseMessage response)
        {
            string result = string.Empty;

            System.Net.Http.HttpContent sm = response.Content;
            Task <Stream> sr  = sm.ReadAsStreamAsync();
            Task <string> res = sm.ReadAsStringAsync();

            result = res.Result;
            return(result);
        }
        private static async Task<HttpContent> DecompressContent(HttpContent compressedContent, ICompressor compressor)
        {
            using (compressedContent)
            {
                MemoryStream decompressed = new MemoryStream();
                await compressor.Decompress(await compressedContent.ReadAsStreamAsync(), decompressed);
                var newContent = new StreamContent(decompressed);
                // copy content type so we know how to load correct formatter
                newContent.Headers.ContentType = compressedContent.Headers.ContentType;

                return newContent;
            }
        }
		/// <inheritdoc />
		public async Task<object> ConvertFromHttpContentAsync(Type resultType, HttpContent httpContent, CancellationToken cancellationToken = default(CancellationToken))
		{
			if (!CanConvertFromHttpContent(resultType, httpContent))
			{
				throw new NotSupportedException("CanConvertFromHttpContent resulted in false, this is not supposed to be called.");
			}
			Log.Debug().WriteLine("Retrieving the content as XDocument, Content-Type: {0}", httpContent.Headers.ContentType);

			using (var contentStream = await httpContent.ReadAsStreamAsync().ConfigureAwait(false))
			{
				return XDocument.Load(contentStream);
			}
		}
예제 #13
0
        /// <summary>
        /// Get all tracks from serverDb
        /// </summary>
        /// <returns></returns>
        public static List <Track> GetTracks()
        {
            var responseTask = client.GetAsync("track");

            responseTask.Wait();

            var result = responseTask.Result;
            IDictionary <string, dynamic> json = null;

            if (result.IsSuccessStatusCode)
            {
                var readTask = result.Content.ReadAsAsync <IDictionary <string, dynamic> >();
                readTask.Wait();
                json = readTask.Result;
            }
            List <Track> tracks = JsonConvert.DeserializeObject <List <Track> >(json["objects"].ToString());

            foreach (var track in tracks)
            {
                string[] splitAlbumPath = track.album.Split('/');
                track.album = splitAlbumPath[splitAlbumPath.Length - 1];

                // < Audio download >
                var httpResponseMessage = dlClient.GetAsync("song_file/" + track.id + "/");
                httpResponseMessage.Wait();

                var resp = httpResponseMessage.Result;
                if (resp.IsSuccessStatusCode)
                {
                    System.Net.Http.HttpContent content = resp.Content;
                    string format = content.Headers.ContentDisposition.FileName.Split('.')[content.Headers.ContentDisposition.FileName.Split('.').Length - 1];
                    format = format.Replace("\"", "");

                    if (format == "wav" || format == "mp3")
                    {
                        Stream contentStream = content.ReadAsStreamAsync().Result; // get the actual content stream

                        string path = new Uri(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase)).LocalPath + "/Data/Tracks/" + track.title + "." + format;

                        using (FileStream fileStream = File.Create(path))
                        {
                            contentStream.Seek(0, SeekOrigin.Begin);
                            contentStream.CopyTo(fileStream);
                            track.audio = path;
                        }
                    }
                }
                // </ Audio download >
            }
            return(tracks);
        }
예제 #14
0
        private static async Task<HttpContent> DecompressContentAsync(HttpContent compressedContent, ICompressor compressor)
        {
            using (compressedContent)
            {
                var decompressed = new MemoryStream();
                await compressor.Decompress(await compressedContent.ReadAsStreamAsync(), decompressed).ConfigureAwait(true);

                decompressed.Position = 0;
                var newContent = new StreamContent(decompressed);

                newContent.Headers.ContentType = compressedContent.Headers.ContentType;
                return newContent;
            }
        }
예제 #15
0
        public async Task <FileDTO> GetExampleFileAsync()
        {
            /*
             * TODO - esse método poderia ser melhorado. Em vez de ler o arquivo do disco,
             * seria mais interessante gerar um arquivo de exemplo com as configurações presentes
             * no appsettings.json referentes à posição e tamanho dos campos. Dessa forma, se mudarmos
             * o layout do arquivo, não temos que alterar o arquivo de "exemplo".
             */
            FileDTO file = new FileDTO();

            file.ContentType = "text/plain";
            file.Extension   = "txt";
            file.Name        = "EXAMPLE.txt";
            file.Content     = new MemoryStream();

            if (this._fileServerSettings.Type == "file")
            {
                using (FileStream fs = new FileStream(this._fileServerSettings.Path, FileMode.Open, FileAccess.Read))
                {
                    await fs.CopyToAsync(file.Content);

                    file.Content.Position = 0;
                }
            }
            else
            {
                using (HttpClient client = new HttpClient())
                {
                    HttpResponseMessage response = await client.GetAsync(this._fileServerSettings.Path);

                    if (response.IsSuccessStatusCode)
                    {
                        System.Net.Http.HttpContent content = response.Content;
                        var contentStream = await content.ReadAsStreamAsync();

                        await contentStream.CopyToAsync(file.Content);

                        file.Content.Position = 0;
                    }
                    else
                    {
                        throw new FileNotFoundException("Impossível obter arquivo do servidor");
                    }
                }
            }

            return(file);
        }
        private static async Task<HttpContent> DecompressContentAsync(HttpContent compressedContent, ICompressor compressor)
        {
            using (compressedContent)
            {
                MemoryStream decompressed = new MemoryStream();
                await compressor.Decompress(await compressedContent.ReadAsStreamAsync(), decompressed).ConfigureAwait(false);
                
                // set position back to 0 so it can be read again
                decompressed.Position = 0;

                var newContent = new StreamContent(decompressed);
                // copy content type so we know how to load correct formatter
                newContent.Headers.ContentType = compressedContent.Headers.ContentType;
                return newContent;
            }
        }
예제 #17
0
 public async Task<object> DeserializeAsync(Type type, HttpContent content) {
   HttpContentHeaders headers = content == null ? null : content.Headers;
   // If content length is 0 then return default value for this type
   if (headers != null) {
     if (headers.ContentLength == 0)
       return ReflectionHelper.GetDefaultValue(type);
     var mediaType = headers.ContentType.MediaType;
     Util.Check(_mediaTypes.Contains(mediaType), "JsonContentSerializer: cannot deserialize response body with content type {0}.", mediaType);
   }
   //Get stream
   var stream = await content.ReadAsStreamAsync();
   Encoding effectiveEncoding = SelectCharacterEncoding(headers);
   using (JsonTextReader jsonTextReader = new JsonTextReader(new StreamReader(stream, effectiveEncoding)) 
        { CloseInput = false, MaxDepth = _maxDepth }) {
     return _serializer.Deserialize(jsonTextReader, type);
   }
 }
예제 #18
0
        public static Object Extract( HttpContent content, Type commandType )
        {
            var read = content.ReadAsAsync( commandType );
            read.Wait();

            //reset the internal stream position to allow the WebAPI pipeline to read it again.
            content.ReadAsStreamAsync()
                .ContinueWith( t =>
                {
                    if( t.Result.CanSeek )
                    {
                        t.Result.Seek( 0, SeekOrigin.Begin );
                    }
                } )
                .Wait();

            return read.Result;
        }
예제 #19
0
        private static async Task GetPRStats(List <stat> stats, ht.HttpClient client, string project)
        {
            var complete = false;
            var url      = $"https://api.github.com/repos/xBimTeam/{project}/pulls";
            int iPage    = 1;

            while (!complete)
            {
                // Get the response.
                var t = url + "?page=" + iPage + "&per_page=30&state=all";
                Debug.WriteLine(t);
                ht.HttpResponseMessage response = await client.GetAsync(t);

                // Get the response content.
                ht.HttpContent responseContent = response.Content;

                // Get the stream of the content.
                using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
                {
                    // Write the output.
                    var content = await reader.ReadToEndAsync();

                    var items = JArray.Parse(content);
                    // JArray comments = (JArray)data["channel"]["item"][0]["category"];
                    foreach (var item in items)
                    {
                        var      user        = item["user"]["login"].ToString();
                        var      created     = item["created_at"].ToString();
                        DateTime createdDate = DateTime.Parse(created);

                        var s = new stat()
                        {
                            login   = user,
                            date    = createdDate,
                            project = project,
                            type    = "PR"
                        };
                        stats.Add(s);
                    }
                    iPage++;
                    complete = (items.Count < 30);
                }
            }
        }
예제 #20
0
        /// <summary>
        /// Reads all body parts within a MIME multipart message using the provided <see cref="MultipartStreamProvider"/> instance
        /// to determine where the contents of each body part is written and <paramref name="bufferSize"/> as read buffer size.
        /// </summary>
        /// <typeparam name="T">The <see cref="MultipartStreamProvider"/> with which to process the data.</typeparam>
        /// <param name="content">An existing <see cref="HttpContent"/> instance to use for the object's content.</param>
        /// <param name="streamProvider">A stream provider providing output streams for where to write body parts as they are parsed.</param>
        /// <param name="bufferSize">Size of the buffer used to read the contents.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
        /// <returns>A <see cref="Task{T}"/> representing the tasks of getting the result of reading the MIME content.</returns>
        public static async Task <T> ReadAsMultipartAsync <T>(this HttpContent content, T streamProvider, int bufferSize,
                                                              CancellationToken cancellationToken) where T : MultipartStreamProvider
        {
            if (content == null)
            {
                throw Error.ArgumentNull("content");
            }

            if (streamProvider == null)
            {
                throw Error.ArgumentNull("streamProvider");
            }

            if (bufferSize < MinBufferSize)
            {
                throw Error.ArgumentMustBeGreaterThanOrEqualTo("bufferSize", bufferSize, MinBufferSize);
            }

            Stream stream;

            try
            {
                stream = await content.ReadAsStreamAsync();
            }
            catch (Exception e)
            {
                throw new IOException(Properties.Resources.ReadAsMimeMultipartErrorReading, e);
            }

            using (var parser = new MimeMultipartBodyPartParser(content, streamProvider))
            {
                byte[] data = new byte[bufferSize];
                MultipartAsyncContext context = new MultipartAsyncContext(stream, parser, data, streamProvider.Contents);

                // Start async read/write loop
                await MultipartReadAsync(context, cancellationToken);

                // Let the stream provider post-process when everything is complete
                await streamProvider.ExecutePostProcessingAsync(cancellationToken);

                return(streamProvider);
            }
        }
예제 #21
0
        public async Task<string> StoreImage(HttpContent content, string path, int advertId, string userId)
        {
            Stream stream = await content.ReadAsStreamAsync();
            string fileName = null;

            try
            {
                Image image = Image.FromStream(stream);
                var extension = ImageFormatUtils.FileExtensionFromEncoder(image.RawFormat);
                fileName = string.Format("{0}{1}{2}{3}{4}", advertId, userId, DateTime.Now.Ticks, random.Next() % 10000007, extension);
                var fullPath = Path.Combine(path, fileName);
                image.Save(fullPath);
            }
            catch
            {
                throw new InvalidDataException();
            }

            return fileName;
        }
        // There are many helper overloads for ReadAs*(). Provide one worker function to ensure the logic is shared.
        //
        // For loosely typed, T = Object, type = specific class.
        // For strongly typed, T == type.GetType()
        private static Task <T> ReadAsAsync <T>(HttpContent content, Type type, IEnumerable <MediaTypeFormatter> formatters, IFormatterLogger formatterLogger)
        {
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (formatters == null)
            {
                throw new ArgumentNullException("formatters");
            }

            ObjectContent objectContent = content as ObjectContent;

            if (objectContent != null && objectContent.Value != null && type.IsAssignableFrom(objectContent.Value.GetType()))
            {
                return(TaskHelpers.FromResult((T)objectContent.Value));
            }

            MediaTypeFormatter   formatter = null;
            MediaTypeHeaderValue mediaType = content.Headers.ContentType;

            if (mediaType != null)
            {
                formatter = new MediaTypeFormatterCollection(formatters).FindReader(type, mediaType);
            }

            if (formatter == null)
            {
                string mediaTypeAsString = mediaType != null ? mediaType.MediaType : Properties.Resources.UndefinedMediaType;
                throw new InvalidOperationException(
                          RS.Format(Properties.Resources.NoReadSerializerAvailable, type.Name, mediaTypeAsString));
            }

            return(content.ReadAsStreamAsync()
                   .Then(stream => formatter.ReadFromStreamAsync(type, stream, content.Headers, formatterLogger)
                         .Then(value => (T)value)));
        }
            private async ValueTask <Stream> CreateContentReadStreamAsyncCore(bool async, CancellationToken cancellationToken)
            {
                if (_contentConsumed)
                {
                    throw new InvalidOperationException(SR.net_http_content_stream_already_read);
                }

                _contentConsumed = true;

                Stream originalStream;

                if (async)
                {
                    originalStream = _originalContent.TryReadAsStream() ?? await _originalContent.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    originalStream = _originalContent.ReadAsStream();
                }
                return(GetDecompressedStream(originalStream));
            }
예제 #24
0
        private static async Task <T> ReadAsAsyncCore <T>(
            HttpContent content,
            Type type,
            IFormatterLogger formatterLogger,
            MediaTypeFormatter formatter,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            Stream stream = await content.ReadAsStreamAsync();

            object result = await formatter.ReadFromStreamAsync(
                type,
                stream,
                content,
                formatterLogger,
                cancellationToken
                );

            return((T)result);
        }
        public static Task <T> ReadAsMultipartAsync <T>(this HttpContent content, T streamProvider, int bufferSize) where T : MultipartStreamProvider
        {
            if (content == null)
            {
                throw Error.ArgumentNull("content");
            }

            if (streamProvider == null)
            {
                throw Error.ArgumentNull("streamProvider");
            }

            if (bufferSize < MinBufferSize)
            {
                throw Error.ArgumentMustBeGreaterThanOrEqualTo("bufferSize", bufferSize, MinBufferSize);
            }

            MimeMultipartBodyPartParser parser = null;

            return(content.ReadAsStreamAsync().Then(stream =>
            {
                TaskCompletionSource <bool> taskCompletionSource = new TaskCompletionSource <bool>();
                parser = new MimeMultipartBodyPartParser(content, streamProvider);
                byte[] data = new byte[bufferSize];
                MultipartAsyncContext context = new MultipartAsyncContext(content, stream, taskCompletionSource, parser, data, streamProvider.Contents);

                // Start async read/write loop
                MultipartReadAsync(context);

                // Return task and complete when we have run the post processing step.
                return taskCompletionSource.Task.Then(() => streamProvider.ExecutePostProcessingAsync().ToTask <T>(result: streamProvider));
            }).Finally(() =>
            {
                if (parser != null)
                {
                    parser.Dispose();
                }
            }, runSynchronously: true));
        }
예제 #26
0
        public async Task <string> DownloadDinosaursFile()
        {
            HttpClientHandler handler = new HttpClientHandler();
            HttpClient        client  = new HttpClient(handler);

            try
            {
                Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
                handler.UseDefaultCredentials = true;
                handler.AllowAutoRedirect     = true;
                HttpResponseMessage response = await client.GetAsync(url);

                response.EnsureSuccessStatusCode();

                if (response.IsSuccessStatusCode)
                {
                    System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
                    System.Net.Http.HttpContent content = response.Content;
                    var contentStream = await content.ReadAsStreamAsync(); // get the actual content stream

                    var excelContent = ParseExcel(contentStream);
                    return(JsonConvert.SerializeObject(excelContent));
                }
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine("\nException Caught!");
                Console.WriteLine("Message :{0} ", e.Message);
            }
            finally
            {
                // Need to call dispose on the HttpClient and HttpClientHandler objects
                // when done using them, so the app doesn't leak resources
                handler.Dispose();
                client.Dispose();
            }

            return(string.Empty);
        }
        HttpContent IHttpContentEncryptor.Encrypt(HttpContent originContent)
        {
            if (originContent == null)
            {
                throw new ArgumentNullException("originContent");
            }

            var originStream = originContent.ReadAsStreamAsync().Result;

            if ( originStream.Length == 0)
            {
                return originContent;
            }

            byte[] originBytes = new byte[originStream.Length];
            originStream.Read(originBytes, 0, originBytes.Length);
            var encodedString = Convert.ToBase64String(originBytes);

            var encodedContent = new StringContent(encodedString);
            encodedContent.Headers.ContentType = originContent.Headers.ContentType;

            return encodedContent;
        }
예제 #28
0
        private async Task <Stream> FinishGetStreamAsync(Task <HttpResponseMessage> getTask)
        {
            HttpResponseMessage httpResponseMessage = await getTask.ConfigureAwait(false);

            httpResponseMessage.EnsureSuccessStatusCode();
            HttpContent content = httpResponseMessage.Content;
            Stream      stream1;

            if (content != null)
            {
                Stream stream2 = content.TryReadAsStream();
                if (stream2 == null)
                {
                    stream2 = await content.ReadAsStreamAsync().ConfigureAwait(false);
                }
                stream1 = stream2;
            }
            else
            {
                stream1 = Stream.Null;
            }
            return(stream1);
        }
예제 #29
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);
     }
 }
예제 #30
0
        private async Task <string> GetStringAsyncCore(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)
                {
#if NET46
                    return(await c.ReadAsStringAsync().ConfigureAwait(false));
#else
                    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 = c.TryReadAsStream() ?? 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));
                            }
                        }
#endif
                }

                // No content to return.
                return(string.Empty);
            }
        }
예제 #31
0
        public static async Task GetFileAsync(string url, string mimetype, string filename, DirectoryInfo sagskatalog)
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(mimetype));
            HttpResponseMessage response = await client.GetAsync(url);

            if (response.IsSuccessStatusCode)
            {
                System.Net.Http.HttpContent content = response.Content;
                var contentStream = await content.ReadAsStreamAsync(); // get the actual content stream

                using (var fileStream = File.Create(filename))
                {
                    contentStream.CopyTo(fileStream);
                }
            }
            else
            {
                _log.Error("Kunne ikke hente fil: " + filename);
                //Log this as an error
                throw new FileNotFoundException(response.ReasonPhrase);
            }
            return;
        }
 private static async Task BufferRequestBodyAsync(IDictionary<string, object> environment, HttpContent content)
 {
     await content.LoadIntoBufferAsync();
     // We need to replace the request body with a buffered stream so that other
     // components can read the stream
     environment[OwinConstants.RequestBodyKey] = await content.ReadAsStreamAsync();
 }
예제 #33
0
 public static async Task<GithubDocument> ParseContent(HttpContent content, LinkFactory linkFactory)
 {
     var stream = await content.ReadAsStreamAsync();
     return new GithubDocument(stream, linkFactory);
 }
        /// <summary>
        /// Reads all body parts within a MIME multipart message using the provided <see cref="MultipartStreamProvider"/> instance
        /// to determine where the contents of each body part is written and <paramref name="bufferSize"/> as read buffer size.
        /// </summary>
        /// <typeparam name="T">The <see cref="MultipartStreamProvider"/> with which to process the data.</typeparam>
        /// <param name="content">An existing <see cref="HttpContent"/> instance to use for the object's content.</param>
        /// <param name="streamProvider">A stream provider providing output streams for where to write body parts as they are parsed.</param>
        /// <param name="bufferSize">Size of the buffer used to read the contents.</param>
        /// <returns>A <see cref="Task{T}"/> representing the tasks of getting the result of reading the MIME content.</returns>
#if NETFX_CORE
        public static async Task <T> ReadAsMultipartAsync <T>(this HttpContent content, T streamProvider, int bufferSize) where T : MultipartStreamProvider
        {
            if (content == null)
            {
                throw Error.ArgumentNull("content");
            }

            if (streamProvider == null)
            {
                throw Error.ArgumentNull("streamProvider");
            }

            if (bufferSize < MinBufferSize)
            {
                throw Error.ArgumentMustBeGreaterThanOrEqualTo("bufferSize", bufferSize, MinBufferSize);
            }

            try
            {
                Stream stream = await content.ReadAsStreamAsync();

                List <HttpContent> childContents = new List <HttpContent>();

                using (var parser = new MimeMultipartBodyPartParser(content, streamProvider))
                {
                    byte[] buffer    = new byte[bufferSize];
                    bool   finalPart = false;

                    while (!finalPart)
                    {
                        int readCount = await stream.ReadAsync(buffer, 0, buffer.Length);

                        // The parser returns one or more parsed parts, depending on how much data was returned
                        // from the network read. The last part may be incomplete (partial), so we only dispose
                        // of the parts once we know they're finished. Regardless of whether the part is complete
                        // or not, we send the bytes to the desired output stream. We loop back for more data
                        // until we've completely read the complete, final part.
                        foreach (MimeBodyPart part in parser.ParseBuffer(buffer, readCount))
                        {
                            try
                            {
                                Stream output = part.GetOutputStream(content);

                                foreach (ArraySegment <byte> segment in part.Segments)
                                {
                                    await output.WriteAsync(segment.Array, segment.Offset, segment.Count);
                                }

                                if (part.IsComplete)
                                {
                                    if (part.HttpContent != null)
                                    {
                                        childContents.Add(part.HttpContent);
                                    }

                                    finalPart = part.IsFinal;
                                    part.Dispose();
                                    break;
                                }
                            }
                            catch (Exception)
                            {
                                // Clean up the part if we got an error in the middle of parsing, because we normally
                                // won't dispose a part until it's complete.
                                part.Dispose();
                                throw;
                            }
                        }
                    }

                    // Let the stream provider post-process when everything is complete
                    await streamProvider.ExecutePostProcessingAsync();

                    return(streamProvider);
                }
            }
            catch (Exception e)
            {
                throw new IOException(Properties.Resources.ReadAsMimeMultipartErrorReading, e);
            }
        }
예제 #35
0
 public static Task <Stream> ReadAsStreamAsync(this HttpContent httpContent, CancellationToken cancellation = default)
 {
     cancellation.ThrowIfCancellationRequested();
     return(httpContent.ReadAsStreamAsync());
 }
        private static async Task <HttpResponseMessage> ReadAsHttpResponseMessageAsyncCore(
            this HttpContent content,
            int bufferSize,
            int maxHeaderSize,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            Stream stream = await content.ReadAsStreamAsync();

            HttpUnsortedResponse     httpResponse = new HttpUnsortedResponse();
            HttpResponseHeaderParser parser       = new HttpResponseHeaderParser(
                httpResponse,
                HttpResponseHeaderParser.DefaultMaxStatusLineSize,
                maxHeaderSize
                );
            ParserState parseStatus;

            byte[] buffer         = new byte[bufferSize];
            int    bytesRead      = 0;
            int    headerConsumed = 0;

            while (true)
            {
                try
                {
                    bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
                }
                catch (Exception e)
                {
                    throw new IOException(Properties.Resources.HttpMessageErrorReading, e);
                }

                try
                {
                    parseStatus = parser.ParseBuffer(buffer, bytesRead, ref headerConsumed);
                }
                catch (Exception)
                {
                    parseStatus = ParserState.Invalid;
                }

                if (parseStatus == ParserState.Done)
                {
                    // Create and return parsed HttpResponseMessage
                    return(CreateHttpResponseMessage(
                               httpResponse,
                               stream,
                               bytesRead - headerConsumed
                               ));
                }
                else if (parseStatus != ParserState.NeedMoreData)
                {
                    throw Error.InvalidOperation(
                              Properties.Resources.HttpMessageParserError,
                              headerConsumed,
                              buffer
                              );
                }
                else if (bytesRead == 0)
                {
                    throw new IOException(
                              Properties.Resources.ReadAsHttpMessageUnexpectedTermination
                              );
                }
            }
        }
예제 #37
0
 /// <summary>
 /// Creates an array copy of the <paramref name="content"/> without affecting the <see cref="Stream.Position"/> of the original object.
 /// </summary>
 private static async Task<byte[]> CloneArrayAsync(HttpContent content)
 {
     var result = await content.ReadAsByteArrayAsync();
     var stream = await content.ReadAsStreamAsync();
     if (stream.CanSeek) stream.Position = 0;
     return result;
 }
        public static Task <HttpRequestMessage> ReadAsHttpRequestMessageAsync(this HttpContent content, string uriScheme, int bufferSize)
        {
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }

            if (uriScheme == null)
            {
                throw new ArgumentNullException("uriScheme");
            }

            if (!Uri.CheckSchemeName(uriScheme))
            {
                throw new ArgumentException(RS.Format(Properties.Resources.HttpMessageParserInvalidUriScheme, uriScheme, typeof(Uri).Name), "uriScheme");
            }

            if (bufferSize < MinBufferSize)
            {
                throw new ArgumentOutOfRangeException("bufferSize", bufferSize, RS.Format(Properties.Resources.ArgumentMustBeGreaterThanOrEqualTo, MinBufferSize));
            }

            HttpMessageContent.ValidateHttpMessageContent(content, true, true);

            return(content.ReadAsStreamAsync().Then(stream =>
            {
                HttpUnsortedRequest httpRequest = new HttpUnsortedRequest();
                HttpRequestHeaderParser parser = new HttpRequestHeaderParser(httpRequest);
                ParserState parseStatus;

                byte[] buffer = new byte[bufferSize];
                int bytesRead = 0;
                int headerConsumed = 0;

                while (true)
                {
                    try
                    {
                        bytesRead = stream.Read(buffer, 0, buffer.Length);
                    }
                    catch (Exception e)
                    {
                        throw new IOException(Properties.Resources.HttpMessageErrorReading, e);
                    }

                    try
                    {
                        parseStatus = parser.ParseBuffer(buffer, bytesRead, ref headerConsumed);
                    }
                    catch (Exception)
                    {
                        parseStatus = ParserState.Invalid;
                    }

                    if (parseStatus == ParserState.Done)
                    {
                        return CreateHttpRequestMessage(uriScheme, httpRequest, stream, bytesRead - headerConsumed);
                    }
                    else if (parseStatus != ParserState.NeedMoreData)
                    {
                        throw new IOException(RS.Format(Properties.Resources.HttpMessageParserError, headerConsumed, buffer));
                    }
                }
            }));
        }
예제 #39
0
        public async Task <IActionResult> DownloadDocument(int?id)
        {
            if (id == null)
            {
                return(StatusCode(StatusCodes.Status400BadRequest));
            }
            var document = context_.Documents.Find(id);

            if (document != null)
            {
                string filename = document.StoragePath;
                try
                { //path: ReadIt/ReadIt/xxx.pdf
                    HttpClient client = new HttpClient();
                    //find old path
                    HttpResponseMessage resp = await client.GetAsync(baseUrl_).ConfigureAwait(false);

                    var files = new List <string>();
                    var index = 0;
                    if (resp.IsSuccessStatusCode)
                    {
                        var json = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);

                        JArray jArr = (JArray)JsonConvert.DeserializeObject(json);
                        foreach (var item in jArr)
                        {
                            files.Add(item.ToString());
                        }
                    }
                    for (int i = 0; i < files.Count; i++)
                    {
                        if (files[i] == document.StoragePath)
                        {
                            index = i;
                        }
                    }

                    if (index != 0)
                    {
                        //download file
                        string display = baseUrl_ + "/" + index;
                        var    resp2   = await client.GetAsync(baseUrl_ + "/" + index).ConfigureAwait(false);

                        System.Net.Http.HttpContent content = resp2.Content; // actually a System.Net.Http.StreamContent instance but you do not need to cast as the actual type does not matter in this case

                        string uploadpath = Directory.GetCurrentDirectory();
                        uploadpath = uploadpath + "\\DownloadBook\\" + document.StoragePath;
                        using (var file = System.IO.File.Create(uploadpath))
                        {                                                          // create a new file to write to
                            var contentStream = await content.ReadAsStreamAsync(); // get the actual content stream

                            await contentStream.CopyToAsync(file);                 // copy that stream to the file stream

                            await file.FlushAsync();                               // flush back to disk before disposing
                        }
                    }
                }
                catch
                {
                    //fail uploading files!
                    //can verify: IsSuccessStatusCode
                }
            }
            return(RedirectToAction("Index"));
        }
예제 #40
0
 public static async Task<Stream> awaitReadFileContents(HttpContent content)
 {
     return await content.ReadAsStreamAsync();
 }
예제 #41
0
 private object JsonParser(HttpContent content)
 {
     var stream = content.ReadAsStreamAsync().Result;
     var streamReader = new StreamReader(stream);
     var textReader = new JsonTextReader(streamReader);
     try
     {
         return JObject.Load(textReader);
     }
     catch
     {
         return JArray.Load(textReader);
     }
     finally
     {
         streamReader.Dispose();
     }
 }
예제 #42
0
 private object HtmlParser(HttpContent content)
 {
     var stream = content.ReadAsStreamAsync().Result;
     return CQ.Create(stream);
 }
예제 #43
0
 private object XmlParser(HttpContent content)
 {
     var stream = content.ReadAsStreamAsync().Result;
     return XElement.Load((Stream)stream);
 }
 public static Task <Stream> ReadAsStreamAsync(this HttpContent content, CancellationToken cancellationToken)
 {
     return(content.ReadAsStreamAsync());
 }
예제 #45
0
파일: Handle.cs 프로젝트: inuplan/intranet
        /// <summary>
        /// Processes the image given in the <see cref="HttpContent"/> into a <see cref="UserImage"/> object.
        /// Resizes the image according to the settings given upon instantiation.
        /// </summary>
        /// <param name="user">The user who initiated the procedure</param>
        /// <returns>A processed image file</returns>
        public async Task<Image> ProcessUserImage(User user, HttpContent fileContent, string description)
        {
            using(fileContent)
            {
                // Get filename
                var fullname = Helpers.GetFilename(fileContent.Headers.ContentDisposition.FileName);
                var filename = fullname.Item1.Replace("\"", string.Empty);
                var extension = fullname.Item2.Replace("\"", string.Empty);
                var mime = fileContent.Headers.ContentType.MediaType;

                using (var fileStream = await fileContent.ReadAsStreamAsync())
                {
                    // Retrieve image
                    var originalImage = d.Image.FromStream(fileStream);
                    var format = originalImage.RawFormat;

                    // Convert image to byte array and set file path
                    var originalData = ConvertToByte(originalImage, format);
                    var origData = new Lazy<byte[]>(() => originalData);
                    var origPath = GetPath(user.Username, originalData, extension);

                    // Calculate medium size
                    var mediumWidth = (int)(originalImage.Width * mediumScaleFactor);
                    var mediumHeight = (int)(originalImage.Height * mediumScaleFactor);

                    // Resize image and set file path
                    var previewImage = ResizeImage(originalImage, mediumWidth, mediumHeight);
                    var previewData = ConvertToByte(previewImage, format);
                    var prevData = new Lazy<byte[]>(() => previewData);
                    var prevPath = GetPath(user.Username, previewData, extension);

                    // Calculate height factor & thumbnail height
                    var thumbnailHeightFactor = ((double)(originalImage.Height) / (double)(originalImage.Width));
                    var thumbnailHeight = (int)(thumbnailWidth * thumbnailHeightFactor);

                    // Resize thumbnail and set file path
                    var thumbnailImage = ResizeImage(originalImage, thumbnailWidth, thumbnailHeight);
                    var thumbnailData = ConvertToByte(thumbnailImage, format);
                    var thumbData = new Lazy<byte[]>(() => thumbnailData);
                    var thumbPath = GetPath(user.Username, thumbnailData, extension);

                    // FileInfos
                    var original = new FileInfo { Data = origData, Path = origPath };
                    var preview = new FileInfo { Data = prevData, Path = prevPath };
                    var thumbnail = new FileInfo { Data = thumbData, Path = thumbPath };

                    // Return the processed image file. Note it has not been saved to the database yet!
                    return new Image
                    {
                        Filename = filename,
                        Extension = extension,
                        MimeType = mime,
                        Description = description,
                        Original = original,
                        Preview = preview,
                        Thumbnail = thumbnail,
                        Owner = user,
                        Uploaded = DateTime.Now,
                    };
                }
            }
        }
예제 #46
0
 /// <summary>
 /// Read and deserialize the HttpContent streaming content
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="content"></param>
 /// <returns></returns>
 public static async Task <T> ReadAsAsync <T>(this HttpContent content) =>
 await JsonSerializer.DeserializeAsync <T>(await content.ReadAsStreamAsync().ConfigureAwait(false)).ConfigureAwait(false);
예제 #47
0
 private async Task<Response> DeserializeResponse(Request request, HttpContent content, Type bodyType)
 {
     using (Stream contentStream = await content.ReadAsStreamAsync())
     {
         using (var reader = new StreamReader(contentStream, Encoding.UTF8, false, DefaultBufferSize, true))
         {
             return DeserializeResponseXml(request, reader, bodyType);
         }
     }
 }
예제 #48
0
        public static Task <HttpResponseMessage> ReadAsHttpResponseMessageAsync(this HttpContent content, int bufferSize, int maxHeaderSize)
        {
            if (content == null)
            {
                throw Error.ArgumentNull("content");
            }

            if (bufferSize < MinBufferSize)
            {
                throw Error.ArgumentMustBeGreaterThanOrEqualTo("bufferSize", bufferSize, MinBufferSize);
            }

            if (maxHeaderSize < InternetMessageFormatHeaderParser.MinHeaderSize)
            {
                throw Error.ArgumentMustBeGreaterThanOrEqualTo("maxHeaderSize", maxHeaderSize, InternetMessageFormatHeaderParser.MinHeaderSize);
            }

            HttpMessageContent.ValidateHttpMessageContent(content, false, true);

            return(content.ReadAsStreamAsync().Then(stream =>
            {
                HttpUnsortedResponse httpResponse = new HttpUnsortedResponse();
                HttpResponseHeaderParser parser = new HttpResponseHeaderParser(httpResponse, HttpResponseHeaderParser.DefaultMaxStatusLineSize, maxHeaderSize);
                ParserState parseStatus;

                byte[] buffer = new byte[bufferSize];
                int bytesRead = 0;
                int headerConsumed = 0;

                while (true)
                {
                    try
                    {
                        bytesRead = stream.Read(buffer, 0, buffer.Length);
                    }
                    catch (Exception e)
                    {
                        throw new IOException(Properties.Resources.HttpMessageErrorReading, e);
                    }

                    try
                    {
                        parseStatus = parser.ParseBuffer(buffer, bytesRead, ref headerConsumed);
                    }
                    catch (Exception)
                    {
                        parseStatus = ParserState.Invalid;
                    }

                    if (parseStatus == ParserState.Done)
                    {
                        // Create and return parsed HttpResponseMessage
                        return CreateHttpResponseMessage(httpResponse, stream, bytesRead - headerConsumed);
                    }
                    else if (parseStatus != ParserState.NeedMoreData)
                    {
                        throw Error.InvalidOperation(Properties.Resources.HttpMessageParserError, headerConsumed, buffer);
                    }
                }
            }));
        }
 /// <summary>
 ///     Serialize the HTTP content and return a stream that represents the content as a synchronous operation.
 /// </summary>
 public static Stream ReadAsStream(this HttpContent content)
 {
     return(Task.Run(() => content.ReadAsStreamAsync()).Result);
 }
예제 #50
0
 public XElementContent(HttpContent content)
 {
     _Element = XElement.Load(content.ReadAsStreamAsync().Result);
 }
예제 #51
0
 DbEmdFile CreateDbFile(HttpContent ctnt, Stream stream)
 {
     DbEmdFile dbFile = new DbEmdFile();
     dbFile.ApplicationUserID = User.Identity.GetUserId();
     // You would get hold of the inner memory stream here
     stream = ctnt.ReadAsStreamAsync().Result;
     dbFile.Content = stream.StreamToByteArray();
     dbFile.ContentType = ctnt.Headers.ContentType.MediaType;
     dbFile.FileName = ctnt.Headers.ContentDisposition.FileName;
     dbFile.FileSize = (long)ctnt.Headers.ContentLength;
     return dbFile;
 }
 private static async Task BufferRequestBodyAsync(IOwinRequest owinRequest, HttpContent content)
 {
     await content.LoadIntoBufferAsync();
     // We need to replace the request body with a buffered stream so that other
     // components can read the stream
     owinRequest.Body = await content.ReadAsStreamAsync();
 }
예제 #53
0
        internal static async Task<rt.IHttpContent> GetContentFromNet(HttpContent content)
        {
            if (content == null)
                return null;

            var stream = await content.ReadAsStreamAsync().ConfigureAwait(false);
            var c = new rt.HttpStreamContent(stream.AsInputStream());

            CopyHeaders(content.Headers, c.Headers);

            return c;
        }
예제 #54
0
        private static async Task <RTIHttpContent> CreateRequestContentAsync(HttpRequestMessage request, HttpRequestHeaderCollection rtHeaderCollection)
        {
            HttpContent content = request.Content;

            RTIHttpContent      rtContent;
            ArraySegment <byte> buffer;

            // If we are buffered already, it is more efficient to send the data directly using the buffer with the
            // WinRT HttpBufferContent class than using HttpStreamContent. This also avoids issues caused by
            // a design limitation in the System.Runtime.WindowsRuntime System.IO.NetFxToWinRtStreamAdapter.
            if (content.TryGetBuffer(out buffer))
            {
                rtContent = new RTHttpBufferContent(buffer.Array.AsBuffer(), (uint)buffer.Offset, (uint)buffer.Count);
            }
            else
            {
                Stream contentStream = await content.ReadAsStreamAsync().ConfigureAwait(false);

                if (contentStream is RTIInputStream)
                {
                    rtContent = new RTHttpStreamContent((RTIInputStream)contentStream);
                }
                else if (contentStream is MemoryStream)
                {
                    var memStream = contentStream as MemoryStream;
                    if (memStream.TryGetBuffer(out buffer))
                    {
                        rtContent = new RTHttpBufferContent(buffer.Array.AsBuffer(), (uint)buffer.Offset, (uint)buffer.Count);
                    }
                    else
                    {
                        byte[] byteArray = memStream.ToArray();
                        rtContent = new RTHttpBufferContent(byteArray.AsBuffer(), 0, (uint)byteArray.Length);
                    }
                }
                else
                {
                    rtContent = new RTHttpStreamContent(contentStream.AsInputStream());
                }
            }

            // RTHttpBufferContent constructor automatically adds a Content-Length header. RTHttpStreamContent does not.
            // Clear any 'Content-Length' header added by the RTHttp*Content objects. We need to clear that now
            // and decide later whether we need 'Content-Length' or 'Transfer-Encoding: chunked' headers based on the
            // .NET HttpRequestMessage and Content header collections.
            rtContent.Headers.ContentLength = null;

            // Deal with conflict between 'Content-Length' vs. 'Transfer-Encoding: chunked' semantics.
            // Desktop System.Net allows both headers to be specified but ends up stripping out
            // 'Content-Length' and using chunked semantics.  The WinRT APIs throw an exception so
            // we need to manually strip out the conflicting header to maintain app compatibility.
            if (request.Headers.TransferEncodingChunked.HasValue && request.Headers.TransferEncodingChunked.Value)
            {
                content.Headers.ContentLength = null;
            }
            else
            {
                // Trigger delayed header generation via TryComputeLength. This code is needed due to an outstanding
                // bug in HttpContentHeaders.ContentLength. See GitHub Issue #5523.
                content.Headers.ContentLength = content.Headers.ContentLength;
            }

            foreach (KeyValuePair <string, IEnumerable <string> > headerPair in content.Headers)
            {
                foreach (string value in headerPair.Value)
                {
                    if (!rtContent.Headers.TryAppendWithoutValidation(headerPair.Key, value))
                    {
                        // rtContent headers are restricted to a white-list of allowed headers, while System.Net.HttpClient's content headers
                        // will allow custom headers.  If something is not successfully added to the content headers, try adding them to the standard headers.
                        bool success = rtHeaderCollection.TryAppendWithoutValidation(headerPair.Key, value);
                        Debug.Assert(success);
                    }
                }
            }
            return(rtContent);
        }
예제 #55
0
 private async Task <byte[]> GetByteArrayAsyncCore(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)
             {
                 long?  contentLength = headers.ContentLength;
                 Stream buffer;
                 if (contentLength.HasValue)
                 {
                     buffer = (Stream) new HttpContent.LimitMemoryStream(this._maxResponseContentBufferSize, (int)contentLength.GetValueOrDefault());
                     try
                     {
                         await responseStream.CopyToAsync(buffer).ConfigureAwait(false);
                     }
                     catch (Exception ex) when(HttpContent.StreamCopyExceptionNeedsWrapping(ex))
                     {
                         throw HttpContent.WrapStreamCopyException(ex);
                     }
                     if (buffer.Length > 0L)
                     {
                         return(((HttpContent.LimitMemoryStream)buffer).GetSizedBuffer());
                     }
                 }
                 else
                 {
                     buffer = (Stream) new HttpContent.LimitArrayPoolWriteStream(this._maxResponseContentBufferSize);
                     try
                     {
                         try
                         {
                             await responseStream.CopyToAsync(buffer).ConfigureAwait(false);
                         }
                         catch (Exception ex) when(HttpContent.StreamCopyExceptionNeedsWrapping(ex))
                         {
                             throw HttpContent.WrapStreamCopyException(ex);
                         }
                         if (buffer.Length > 0L)
                         {
                             return(((HttpContent.LimitArrayPoolWriteStream)buffer).ToArray());
                         }
                     }
                     finally
                     {
                         buffer.Dispose();
                     }
                 }
                 buffer = (Stream)null;
             }
             headers = (HttpContentHeaders)null;
         }
         return(Array.Empty <byte>());
     }
 }