using System.Net.Http; using System.Text; using XFEExtension.NetCore.TaskExtension; namespace XFEExtension.NetCore.WebExtension; /// /// 网络拓展 /// public static class WebExtension { /// /// 从URL获取字符串内容 /// /// /// public static async Task GetFromURLAsync(this string url) { if (string.IsNullOrEmpty(url)) { throw new ArgumentException($"“{nameof(url)}”不能为 null 或空。", nameof(url)); } using var 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) { if (string.IsNullOrEmpty(url)) { throw new ArgumentException($"“{nameof(url)}”不能为 null 或空。", nameof(url)); } using var 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) { if (string.IsNullOrEmpty(url)) { throw new ArgumentException($"“{nameof(url)}”不能为 null 或空。", nameof(url)); } using var 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) { if (string.IsNullOrEmpty(url)) { throw new ArgumentException($"“{nameof(url)}”不能为 null 或空。", nameof(url)); } using var 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) { if (string.IsNullOrEmpty(url)) { throw new ArgumentException($"“{nameof(url)}”不能为 null 或空。", nameof(url)); } using var 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) { if (string.IsNullOrEmpty(url)) { throw new ArgumentException($"“{nameof(url)}”不能为 null 或空。", nameof(url)); } using var 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; } } /// /// 通过给定的URL获取其文件名 /// /// 指定的URL /// public static async Task GetFileNameFromURL(this string url) { var response = await new HttpClient().GetAsync(url, HttpCompletionOption.ResponseHeadersRead); response.EnsureSuccessStatusCode(); if (response.Content.Headers.ContentDisposition?.FileName is null) return Path.GetFileName(response.RequestMessage?.RequestUri?.AbsolutePath); else return response.Content.Headers.ContentDisposition.FileNameStar; } }