using System; using System.IO; using System.Net; using System.Text; using System.Threading.Tasks; 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/91.0.4472.124 Safari/537.36 Edg/91.0.864.67"; static WebUtil() { ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; } private static int DefaultGetTimeout => Global.Settings.RequestTimeout; public static HttpWebRequest CreateRequest(string url, int? timeout = null, string? userAgent = null) { var req = (HttpWebRequest)WebRequest.Create(url); req.UserAgent = string.IsNullOrWhiteSpace(userAgent) ? DefaultUserAgent : userAgent; req.Accept = "*/*"; req.KeepAlive = true; req.Timeout = timeout ?? DefaultGetTimeout; req.ReadWriteTimeout = timeout ?? DefaultGetTimeout; req.Headers.Add("Accept-Charset", "utf-8"); return req; } /// /// 异步下载 /// /// /// public static async Task DownloadBytesAsync(HttpWebRequest req) { using var webResponse = req.GetResponseAsync(); await using var memoryStream = new MemoryStream(); await using var input = webResponse.Result.GetResponseStream(); await input.CopyToAsync(memoryStream); return memoryStream.ToArray(); } /// /// 异步下载并编码为字符串 /// /// /// /// 编码,默认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); return streamReader.ReadToEnd(); } /// /// 异步下载并编码为字符串 /// /// /// 编码,默认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); return await streamReader.ReadToEndAsync(); } 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) { 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(); await downloadTask; } progress?.Report(100); } private static async Task ReportProgress(long total, IAsyncResult downloadTask, Stream stream, IProgress progress, int interval) { var n = 0; while (!downloadTask.IsCompleted) { var n1 = (int)((double)stream.Length / total * 100); if (n != n1) { n = n1; progress.Report(n); } await Task.Delay(interval).ConfigureAwait(false); } } } }