public async static Task DownloadClashTestResource(this ClashTestResource resource, Func <Stream, Task> downloadStreamProcessor, bool decompress = false) { using (var request = new HttpRequestMessage(HttpMethod.Get, resource.Url)) { foreach (var kvp in resource.Headers) { request.Headers.TryAddWithoutValidation(kvp.Key, kvp.Value); } using (var client = new HttpClient()) { var response = await client.SendAsync(request, CancellationToken.None); response.EnsureSuccessStatusCode(); using (var res = await response.Content.ReadAsStreamAsync()) { if (decompress) { using (var dc = new GZipStream(res, CompressionMode.Decompress)) { await downloadStreamProcessor(dc); } } else { await downloadStreamProcessor(res); } } } } }
public static async Task DownloadClashTestResource(this ClashTestResource resource, FileInfo destination, bool decompress = false) { await DownloadClashTestResource( resource, async resultStream => { using (var fout = destination.Open(FileMode.Create)) { await resultStream.CopyToAsync(fout); } }, decompress); }