Skip to content

Commit 60db8e8

Browse files
authored
Replace WebClient usage with HttpClient (#6388)
* Replace WebClient usage with HttpClient * Fix test * Attempt to fix sync over async
1 parent 9ba2cd7 commit 60db8e8

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

test/Microsoft.ML.AutoML.Tests/DatasetUtil.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.IO.Compression;
99
using System.Linq;
1010
using System.Net;
11+
using System.Net.Http;
12+
using System.Threading.Tasks;
1113
using Microsoft.ML.Data;
1214
using Microsoft.ML.TestFrameworkCommon;
1315

@@ -185,13 +187,13 @@ public static string DownloadImageSet(string imagesDownloadFolder)
185187
string fileName = "flower_photos_tiny_set_for_unit_tests.zip";
186188
string url = $"https://aka.ms/mlnet-resources/datasets/flower_photos_tiny_set_for_unit_test.zip";
187189

188-
Download(url, imagesDownloadFolder, fileName);
190+
Download(url, imagesDownloadFolder, fileName).Wait();
189191
UnZip(Path.Combine(imagesDownloadFolder, fileName), imagesDownloadFolder);
190192

191193
return Path.GetFileNameWithoutExtension(fileName);
192194
}
193195

194-
private static void Download(string url, string destDir, string destFileName)
196+
private static async Task Download(string url, string destDir, string destFileName)
195197
{
196198
if (destFileName == null)
197199
destFileName = Path.GetFileName(new Uri(url).AbsolutePath); ;
@@ -203,7 +205,16 @@ private static void Download(string url, string destDir, string destFileName)
203205
if (File.Exists(relativeFilePath))
204206
return;
205207

206-
new WebClient().DownloadFile(url, relativeFilePath);
208+
using (var client = new HttpClient())
209+
{
210+
var response = await client.GetAsync(url).ConfigureAwait(false);
211+
var stream = await response.EnsureSuccessStatusCode().Content.ReadAsStreamAsync().ConfigureAwait(false);
212+
var fileInfo = new FileInfo(relativeFilePath);
213+
using (var fileStream = fileInfo.OpenWrite())
214+
{
215+
await stream.CopyToAsync(fileStream).ConfigureAwait(false);
216+
}
217+
}
207218
return;
208219
}
209220

0 commit comments

Comments
 (0)