示例#1
0
        // http://www.tugberkugurlu.com/archive/efficiently-streaming-large-http-responses-with-httpclient
        protected async Task DownloadToStream(string path, Stream target)
        {
            using (var pool = new HttpClientPool())
            {
                try
                {
                    var client = pool.NewClient();
                    var result = await client.GetAsync(new Uri(ChainEndPoint, path), HttpCompletionOption.ResponseHeadersRead);

                    result.EnsureSuccessStatusCode();

                    using (var source = await result.Content.ReadAsStreamAsync())
                    {
                        await source.CopyToAsync(target);

                        return;
                    }
                    throw new DownloadInvalidContentTypeException(ChainEndPoint, path);
                }
                catch (TaskCanceledException)
                {
                    throw new DownloadTimeoutException(ChainEndPoint, path);
                }
                catch (Exception)
                {
                    throw new DownloadNotFoundException(ChainEndPoint, path);
                }
            }
        }
示例#2
0
        protected async Task <string> DownloadText(string path, bool checkMediaType = true)
        {
            using (var pool = new HttpClientPool())
            {
                try
                {
                    var client = pool.NewClient();
                    var result = await client.GetAsync(new Uri(ChainEndPoint, path));

                    result.EnsureSuccessStatusCode();
                    if (!checkMediaType || result.Content.Headers.ContentType.MediaType == "text/plain")
                    {
                        return(await result.Content.ReadAsStringAsync());
                    }
                    throw new DownloadInvalidContentTypeException(ChainEndPoint, path);
                }
                catch (TaskCanceledException)
                {
                    throw new DownloadTimeoutException(ChainEndPoint, path);
                }
                catch (Exception)
                {
                    throw new DownloadNotFoundException(ChainEndPoint, path);
                }
            }
        }
示例#3
0
        protected async Task <byte[]> DownloadBinary(string path, bool checkMediaType = true)
        {
            using (var pool = new HttpClientPool())
            {
                try
                {
                    var client = pool.NewClient();
                    var uri    = new Uri(ChainEndPoint, path);
                    var result = await client.GetAsync(uri);

                    result.EnsureSuccessStatusCode();
                    if (!checkMediaType || result.Content.Headers.ContentType.MediaType == "application/octet-stream")
                    {
                        return(await result.Content.ReadAsByteArrayAsync());
                    }
                    throw new DownloadInvalidContentTypeException(ChainEndPoint, path);
                }
                catch (TaskCanceledException)
                {
                    throw new DownloadTimeoutException(ChainEndPoint, path);
                }
                catch (Exception)
                {
                    throw new DownloadNotFoundException(ChainEndPoint, path);
                }
            }
        }