diff --git a/Netch/Utils/WebUtil.cs b/Netch/Utils/WebUtil.cs
index 52bb30ac..10e50238 100644
--- a/Netch/Utils/WebUtil.cs
+++ b/Netch/Utils/WebUtil.cs
@@ -9,7 +9,7 @@ namespace Netch.Utils
public static class WebUtil
{
public const string DefaultUserAgent =
- @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.55";
+ @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67";
static WebUtil()
{
@@ -52,11 +52,12 @@ namespace Netch.Utils
///
/// 编码,默认UTF-8
///
- public static string DownloadString(HttpWebRequest req, out HttpWebResponse rep, string encoding = "UTF-8")
+ public static string DownloadString(HttpWebRequest req, out HttpWebResponse rep, Encoding? encoding = null)
{
+ encoding ??= Encoding.UTF8;
rep = (HttpWebResponse)req.GetResponse();
using var responseStream = rep.GetResponseStream();
- using var streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encoding));
+ using var streamReader = new StreamReader(responseStream, encoding);
return streamReader.ReadToEnd();
}
@@ -67,51 +68,38 @@ namespace Netch.Utils
///
/// 编码,默认UTF-8
///
- public static async Task DownloadStringAsync(HttpWebRequest req, string encoding = "UTF-8")
+ public static async Task DownloadStringAsync(HttpWebRequest req, Encoding? encoding = null)
{
+ encoding ??= Encoding.UTF8;
using var webResponse = await req.GetResponseAsync();
await using var responseStream = webResponse.GetResponseStream();
- using var streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encoding));
+ using var streamReader = new StreamReader(responseStream, encoding);
return await streamReader.ReadToEndAsync();
}
- ///
- /// 异步下载到文件
- ///
- ///
- ///
- ///
- public static async Task DownloadFileAsync(HttpWebRequest req, string fileFullPath)
- {
- using var webResponse = (HttpWebResponse)await req.GetResponseAsync();
- await using var input = webResponse.GetResponseStream();
- await using var fileStream = File.OpenWrite(fileFullPath);
-
- await input.CopyToAsync(fileStream);
- fileStream.Flush();
- }
-
- public static async Task DownloadFileAsync(string address, string fileFullPath, IProgress progress)
+ public static async Task DownloadFileAsync(string address, string fileFullPath, IProgress? progress = null)
{
await DownloadFileAsync(CreateRequest(address), fileFullPath, progress);
}
- public static async Task DownloadFileAsync(HttpWebRequest req, string fileFullPath, IProgress progress)
+ public static async Task DownloadFileAsync(HttpWebRequest req, string fileFullPath, IProgress? progress)
{
- using var webResponse = (HttpWebResponse)await req.GetResponseAsync();
- await using var input = webResponse.GetResponseStream();
- await using var fileStream = File.OpenWrite(fileFullPath);
+ await using (var fileStream = File.Open(fileFullPath, FileMode.Create, FileAccess.Write))
+ using (var webResponse = (HttpWebResponse)await req.GetResponseAsync())
+ await using (var input = webResponse.GetResponseStream())
+ using (var downloadTask = input.CopyToAsync(fileStream))
+ {
+ if (progress != null)
+ ReportProgress(webResponse.ContentLength, downloadTask, fileStream, progress, 200).Forget();
- var task = input.CopyToAsync(fileStream);
- ReportStream(webResponse.ContentLength, task, fileStream, progress, 200).Forget();
+ await downloadTask;
+ }
- await task;
- fileStream.Flush();
- progress.Report(100);
+ progress?.Report(100);
}
- private static async Task ReportStream(long total, IAsyncResult downloadTask, Stream stream, IProgress progress, int interval)
+ private static async Task ReportProgress(long total, IAsyncResult downloadTask, Stream stream, IProgress progress, int interval)
{
var n = 0;
while (!downloadTask.IsCompleted)
@@ -123,7 +111,7 @@ namespace Netch.Utils
progress.Report(n);
}
- await Task.Delay(interval);
+ await Task.Delay(interval).ConfigureAwait(false);
}
}
}