Beispiel #1
0
        private async Task <DownloadResult> DownloadUrl(DownloadInstruction instruction)
        {
            using (HttpClient client = new HttpClient())
            {
                //prepare request headers
                if (instruction.ContainRequestHeader)
                {
                    ModifyRequestHeader(client.DefaultRequestHeaders, instruction.RequestHeader);
                }

                //Now that we are set and ready, go and do the download call
                using (HttpResponseMessage response = await client.GetAsync(instruction.Uri))
                {
                    using (HttpContent content = response.Content)
                    {
                        Stream result = await content.ReadAsStreamAsync();

                        MemoryStream mem = new MemoryStream();
                        result.CopyTo(mem);

                        Dictionary <string, string> headers = content.Headers.ToDictionary(
                            x => CultureInfo.CurrentCulture.TextInfo.ToTitleCase(x.Key), x => string.Join(",", x.Value));
                        return(new DownloadResult(instruction.Uri, mem, headers));
                    }
                }
            }
        }
Beispiel #2
0
        private async Task<DownloadResult> DownloadUrl(DownloadInstruction instruction)
        {
            using (HttpClient client = new HttpClient())
            {
                //prepare request headers
                if (instruction.ContainRequestHeader)
                {
                    ModifyRequestHeader(client.DefaultRequestHeaders, instruction.RequestHeader);
                }

                //Now that we are set and ready, go and do the download call
                using (HttpResponseMessage response = await client.GetAsync(instruction.Uri))
                {
                    using (HttpContent content = response.Content)
                    {
                        Stream result = await content.ReadAsStreamAsync();

                        MemoryStream mem = new MemoryStream();
                        result.CopyTo(mem);

                        Dictionary<string, string> headers = content.Headers.ToDictionary(
                            x => CultureInfo.CurrentCulture.TextInfo.ToTitleCase(x.Key), x => string.Join(",", x.Value));
                        return new DownloadResult(instruction.Uri, mem, headers);
                    }
                }
            }
        }