mirror of
https://github.com/netchx/netch.git
synced 2026-03-18 18:13:21 +08:00
Update WebUtil.cs
This commit is contained in:
@@ -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
|
||||
/// <param name="rep"></param>
|
||||
/// <param name="encoding">编码,默认UTF-8</param>
|
||||
/// <returns></returns>
|
||||
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
|
||||
/// <param name="req"></param>
|
||||
/// <param name="encoding">编码,默认UTF-8</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<string> DownloadStringAsync(HttpWebRequest req, string encoding = "UTF-8")
|
||||
public static async Task<string> 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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步下载到文件
|
||||
/// </summary>
|
||||
/// <param name="req"></param>
|
||||
/// <param name="fileFullPath"></param>
|
||||
/// <returns></returns>
|
||||
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<int> progress)
|
||||
public static async Task DownloadFileAsync(string address, string fileFullPath, IProgress<int>? progress = null)
|
||||
{
|
||||
await DownloadFileAsync(CreateRequest(address), fileFullPath, progress);
|
||||
}
|
||||
|
||||
public static async Task DownloadFileAsync(HttpWebRequest req, string fileFullPath, IProgress<int> progress)
|
||||
public static async Task DownloadFileAsync(HttpWebRequest req, string fileFullPath, IProgress<int>? 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<int> progress, int interval)
|
||||
private static async Task ReportProgress(long total, IAsyncResult downloadTask, Stream stream, IProgress<int> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user