using System; using System.IO; using System.Net.Http; using System.Text; using System.Threading.Tasks; using XFEExtension.DelegateExtension; using XFEExtension.TaskExtension; namespace XFEExtension.WebExtension { /// /// 网络拓展 /// public static class WebExtension { /// /// 从URL获取字符串内容 /// /// /// public static async Task GetFromURLAsync(this string url) { using (HttpClient httpClient = new HttpClient()) { var response = await httpClient.GetAsync(url); if (response.IsSuccessStatusCode) { return await response.Content.ReadAsStringAsync(); } else { return null; } } } /// /// 从URL获取字符串内容 /// /// /// public static string GetFromURL(this string url) { using (HttpClient httpClient = new HttpClient()) { var response = httpClient.GetAsync(url).WaitAndGetResult(); if (response.IsSuccessStatusCode) { return response.Content.ReadAsStringAsync().Result; } else { return null; } } } /// /// 从URL获取字符串内容 /// /// /// POST的内容 /// public static async Task PostToURLAsync(this string url, string content) { using (HttpClient httpClient = new HttpClient()) { var response = await httpClient.PostAsync(url, new StringContent(content)); if (response.IsSuccessStatusCode) { return await response.Content.ReadAsStringAsync(); } else { return null; } } } /// /// 从URL获取字符串内容 /// /// /// POST的内容 /// public static string PostToURL(this string url, string content) { using (HttpClient httpClient = new HttpClient()) { var response = httpClient.PostAsync(url, new StringContent(content)).WaitAndGetResult(); if (response.IsSuccessStatusCode) { return response.Content.ReadAsStringAsync().Result; } else { return null; } } } /// /// 从URL获取字符串内容 /// /// /// POST的内容 /// POST的类型 /// public static async Task PostToURLAsync(this string url, string content, string contentType) { using (HttpClient httpClient = new HttpClient()) { var response = await httpClient.PostAsync(url, new StringContent(content, Encoding.UTF8, contentType)); if (response.IsSuccessStatusCode) { return await response.Content.ReadAsStringAsync(); } else { return null; } } } /// /// 从URL获取字符串内容 /// /// /// POST的内容 /// POST的类型 /// public static string PostToURL(this string url, string content, string contentType) { using (HttpClient httpClient = new HttpClient()) { var response = httpClient.PostAsync(url, new StringContent(content, Encoding.UTF8, contentType)).WaitAndGetResult(); if (response.IsSuccessStatusCode) { return response.Content.ReadAsStringAsync().Result; } else { return null; } } } } /// /// XFE下载器 /// public class XFEDownloader { /// /// 字节下载事件 /// public event XFEEventHandler BufferDownloaded; /// /// 目标下载地址 /// public string DownloadUrl { get; set; } /// /// 储存位置 /// public string SavePath { get; set; } /// /// 已下载 /// public bool Downloaded { get; } /// /// 开始下载 /// /// /// public async Task Download(bool continueFromLastDownload = true) { using (var client = new HttpClient()) { var continueDownload = continueFromLastDownload && File.Exists(SavePath); using (var fileStream = new FileStream(SavePath, continueDownload ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.None, 8192, true)) { long totalRead = 0; long lastBufferDownloadSize = fileStream.Length; if (continueDownload) { client.DefaultRequestHeaders.Range = new System.Net.Http.Headers.RangeHeaderValue(lastBufferDownloadSize, null); totalRead = lastBufferDownloadSize; } using (var response = await client.GetAsync(DownloadUrl, HttpCompletionOption.ResponseHeadersRead)) { response.EnsureSuccessStatusCode(); long? totalFileSize = response.Content.Headers.ContentLength + lastBufferDownloadSize; using (var contentStream = await response.Content.ReadAsStreamAsync()) { byte[] buffer = new byte[8192]; int currentRead; while ((currentRead = await contentStream.ReadAsync(buffer, 0, buffer.Length)) > 0) { await fileStream.WriteAsync(buffer, 0, currentRead); totalRead += currentRead; var bufferCopy = new byte[8192]; bufferCopy.CopyTo(buffer, 0); BufferDownloaded?.Invoke(this, new FileDownloadedEventArgs(bufferCopy, totalRead, totalFileSize, currentRead != 8192)); } } } } } } } /// /// 字符下载事件 /// public class FileDownloadedEventArgs : EventArgs { /// /// 当前下载到的字节 /// public byte[] CurrentBuffer { get; } /// /// 总计下载的字节长度 /// public long DownloadedBufferSize { get; } /// /// 总共需要下载的字节大小 /// public long? TotalBufferSize { get; } /// /// 是否下载完成 /// public bool Downloaded { get; } /// /// 字符下载事件 /// /// 当前下载的字符 /// 已下载的字节大小 /// 总计需要下载的字节大小 /// 是否已经下载完成 public FileDownloadedEventArgs(byte[] currentBuffer, long downloadedBufferSize, long? totalBufferSize, bool downloaded) { this.CurrentBuffer = currentBuffer; this.DownloadedBufferSize = downloadedBufferSize; this.TotalBufferSize = totalBufferSize; this.Downloaded = downloaded; } } }