بررسی نکات دریافت فایلهای حجیم توسط HttpClient
نویسنده: وحید نصیری
تاریخ: ۱۳۹۶/۱۰/۲۴ ۱۰:۴۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace HttpClientTips.LargeFiles
{
class Program
{
private static readonly HttpClient _client = new HttpClient();
static async Task Main(string[] args)
{
var bytes = await DownloadLargeFileAsync();
}
public static async Task<byte[]> DownloadLargeFileAsync()
{
Console.WriteLine("Downloading a 4K content - too much bytes.");
var response = await _client.GetAsync("http://downloads.4ksamples.com/downloads/sample-Elysium.2013.2160p.mkv");
var bytes = await response.Content.ReadAsByteArrayAsync();
return bytes;
}
}
} private static readonly HttpClient _client = new HttpClient
{
Timeout = Timeout.InfiniteTimeSpan
}; namespace HttpClientTips.LargeFiles
{
class Program
{
private static readonly HttpClient _client = new HttpClient
{
Timeout = Timeout.InfiniteTimeSpan
};
static async Task Main(string[] args)
{
await DownloadLargeFileAsync();
}
public static async Task DownloadLargeFileAsync()
{
Console.WriteLine("Downloading a 4K content. too much bytes.");
var response = await _client.GetAsync("http://downloads.4ksamples.com/downloads/sample-Elysium.2013.2160p.mkv");
using (var streamToReadFrom = await response.Content.ReadAsStreamAsync())
{
string fileToWriteTo = Path.GetTempFileName();
Console.WriteLine($"Save path: {fileToWriteTo}");
using (var streamToWriteTo = File.Open(fileToWriteTo, FileMode.Create))
{
await streamToReadFrom.CopyToAsync(streamToWriteTo);
}
}
}
}
} var response = await _client.GetAsync(
"http://downloads.4ksamples.com/downloads/sample-Elysium.2013.2160p.mkv",
HttpCompletionOption.ResponseHeadersRead); private static readonly HttpClientHandler _handler = new HttpClientHandler
{
MaxConnectionsPerServer = int.MaxValue, // default for .NET Core
UseDefaultCredentials = true
};
private static readonly HttpClient _client = new HttpClient(_handler)
{
Timeout = Timeout.InfiniteTimeSpan
}; PM> Install-Package DNTCommon.Web.Core