Files
Convention-CSharp/Convention/Runtime/Web.cs

356 lines
9.0 KiB
C#
Raw Normal View History

2025-06-29 15:52:06 +08:00
using System;
using System.Collections.Generic;
using System.IO;
2025-06-29 16:51:35 +08:00
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
2025-06-29 15:52:06 +08:00
namespace Convention
{
[Serializable]
2025-06-29 16:51:35 +08:00
public sealed class ToolURL
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
private string url;
private static readonly HttpClient httpClient = new();
private object data;
2025-06-29 15:52:06 +08:00
2025-06-29 16:51:35 +08:00
public ToolURL(string url)
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
this.url = url;
2025-06-29 15:52:06 +08:00
}
public override string ToString()
{
return this.url;
}
2025-06-29 16:51:35 +08:00
#region HTTP Methods
public async Task<bool> GetAsync(Action<HttpResponseMessage> callback)
2025-06-29 15:52:06 +08:00
{
if (!IsValid)
return false;
2025-06-29 16:51:35 +08:00
try
{
var response = await httpClient.GetAsync(this.url);
callback(response);
return response.IsSuccessStatusCode;
}
catch
2025-06-29 15:52:06 +08:00
{
callback(null);
2025-06-29 16:51:35 +08:00
return false;
2025-06-29 15:52:06 +08:00
}
}
2025-06-29 16:51:35 +08:00
public bool Get(Action<HttpResponseMessage> callback)
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
return GetAsync(callback).GetAwaiter().GetResult();
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
public async Task<bool> PostAsync(Action<HttpResponseMessage> callback, Dictionary<string, string> formData = null)
2025-06-29 15:52:06 +08:00
{
if (!IsValid)
return false;
2025-06-29 16:51:35 +08:00
try
{
HttpContent content = null;
if (formData != null)
{
content = new FormUrlEncodedContent(formData);
}
2025-06-29 15:52:06 +08:00
2025-06-29 16:51:35 +08:00
var response = await httpClient.PostAsync(this.url, content);
callback(response);
return response.IsSuccessStatusCode;
}
catch
2025-06-29 15:52:06 +08:00
{
callback(null);
2025-06-29 16:51:35 +08:00
return false;
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
}
2025-06-29 15:52:06 +08:00
2025-06-29 16:51:35 +08:00
public bool Post(Action<HttpResponseMessage> callback, Dictionary<string, string> formData = null)
{
return PostAsync(callback, formData).GetAwaiter().GetResult();
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
#endregion
2025-06-29 15:52:06 +08:00
#region URL Properties
2025-06-29 16:51:35 +08:00
2025-06-29 15:52:06 +08:00
public string FullURL => this.url;
public static implicit operator string(ToolURL data) => data.FullURL;
2025-06-29 16:51:35 +08:00
2025-06-29 15:52:06 +08:00
public string GetFullURL()
{
return this.url;
}
public string GetFilename()
{
if (string.IsNullOrEmpty(this.url))
return "";
Uri uri = new Uri(this.url);
string path = uri.AbsolutePath;
return Path.GetFileName(path);
}
public string GetExtension()
{
string filename = GetFilename();
if (string.IsNullOrEmpty(filename))
return "";
return Path.GetExtension(filename);
}
public bool ExtensionIs(params string[] extensions)
{
string el = GetExtension().ToLower();
string eln = el.Length > 1 ? el[1..] : null;
foreach (string extension in extensions)
if (el == extension || eln == extension)
return true;
return false;
}
2025-06-29 16:51:35 +08:00
2025-06-29 15:52:06 +08:00
#endregion
#region Validation
2025-06-29 16:51:35 +08:00
2025-06-29 15:52:06 +08:00
public bool IsValid => ValidateURL();
2025-06-29 16:51:35 +08:00
2025-06-29 15:52:06 +08:00
public bool ValidateURL()
{
if (string.IsNullOrEmpty(this.url))
return false;
return Uri.TryCreate(this.url, UriKind.Absolute, out Uri uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
}
2025-06-29 16:51:35 +08:00
public static implicit operator bool(ToolURL url) => url.IsValid;
2025-06-29 15:52:06 +08:00
2025-06-29 16:51:35 +08:00
#endregion
2025-06-29 15:52:06 +08:00
2025-06-29 16:51:35 +08:00
#region Load Methods
2025-06-29 15:52:06 +08:00
2025-06-29 16:51:35 +08:00
public async Task<string> LoadAsTextAsync()
2025-06-29 15:52:06 +08:00
{
if (!IsValid)
return null;
2025-06-29 16:51:35 +08:00
try
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
var response = await httpClient.GetAsync(this.url);
if (response.IsSuccessStatusCode)
{
this.data = await response.Content.ReadAsStringAsync();
return (string)this.data;
}
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
catch
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
// 请求失败
2025-06-29 15:52:06 +08:00
}
return null;
}
2025-06-29 16:51:35 +08:00
public string LoadAsText()
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
return LoadAsTextAsync().GetAwaiter().GetResult();
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
public async Task<byte[]> LoadAsBinaryAsync()
2025-06-29 15:52:06 +08:00
{
if (!IsValid)
return null;
2025-06-29 16:51:35 +08:00
try
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
var response = await httpClient.GetAsync(this.url);
if (response.IsSuccessStatusCode)
{
this.data = await response.Content.ReadAsByteArrayAsync();
return (byte[])this.data;
}
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
catch
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
// 请求失败
2025-06-29 15:52:06 +08:00
}
return null;
}
2025-06-29 16:51:35 +08:00
public byte[] LoadAsBinary()
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
return LoadAsBinaryAsync().GetAwaiter().GetResult();
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
public T LoadAsJson<T>()
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
string jsonText = LoadAsText();
if (string.IsNullOrEmpty(jsonText))
return default(T);
2025-06-29 15:52:06 +08:00
2025-06-29 16:51:35 +08:00
try
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
T result = JsonSerializer.Deserialize<T>(jsonText);
this.data = result;
return result;
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
catch
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
return default(T);
2025-06-29 15:52:06 +08:00
}
}
2025-06-29 16:51:35 +08:00
public async Task<T> LoadAsJsonAsync<T>()
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
string jsonText = await LoadAsTextAsync();
if (string.IsNullOrEmpty(jsonText))
return default(T);
2025-06-29 15:52:06 +08:00
2025-06-29 16:51:35 +08:00
try
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
T result = JsonSerializer.Deserialize<T>(jsonText);
this.data = result;
return result;
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
catch
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
return default(T);
2025-06-29 15:52:06 +08:00
}
}
#endregion
2025-06-29 16:51:35 +08:00
#region Save Methods
public void Save(string localPath = null)
2025-06-29 15:52:06 +08:00
{
if (IsText)
SaveAsText(localPath);
else if (IsJson)
SaveAsJson(localPath);
else
SaveAsBinary(localPath);
}
2025-06-29 16:51:35 +08:00
public void SaveAsText(string localPath = null)
2025-06-29 15:52:06 +08:00
{
if (localPath == null)
{
2025-06-29 16:51:35 +08:00
localPath = Path.Combine(Path.GetTempPath(), GetFilename());
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
if (this.data is string text)
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
File.WriteAllText(localPath, text);
2025-06-29 15:52:06 +08:00
}
}
2025-06-29 16:51:35 +08:00
public void SaveAsJson(string localPath = null)
2025-06-29 15:52:06 +08:00
{
if (localPath == null)
{
2025-06-29 16:51:35 +08:00
localPath = Path.Combine(Path.GetTempPath(), GetFilename());
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
if (this.data != null)
{
string jsonText = JsonSerializer.Serialize(this.data);
File.WriteAllText(localPath, jsonText);
}
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
public void SaveAsBinary(string localPath = null)
2025-06-29 15:52:06 +08:00
{
if (localPath == null)
{
2025-06-29 16:51:35 +08:00
localPath = Path.Combine(Path.GetTempPath(), GetFilename());
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
if (this.data is byte[] bytes)
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
File.WriteAllBytes(localPath, bytes);
}
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
#endregion
2025-06-29 15:52:06 +08:00
#region URL Types
2025-06-29 16:51:35 +08:00
2025-06-29 15:52:06 +08:00
public bool IsText => ExtensionIs("txt", "html", "htm", "css", "js", "xml", "csv");
2025-06-29 16:51:35 +08:00
public bool IsJson => ExtensionIs("json");
public bool IsImage => ExtensionIs("jpg", "jpeg", "png", "gif", "bmp", "svg");
public bool IsDocument => ExtensionIs("pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx");
2025-06-29 15:52:06 +08:00
#endregion
#region Operators
2025-06-29 16:51:35 +08:00
public static ToolURL operator |(ToolURL left, string rightPath)
2025-06-29 15:52:06 +08:00
{
string baseUrl = left.GetFullURL();
2025-06-29 16:51:35 +08:00
if (baseUrl.EndsWith('/'))
2025-06-29 15:52:06 +08:00
{
return new ToolURL(baseUrl + rightPath);
}
else
{
return new ToolURL(baseUrl + "/" + rightPath);
}
}
2025-06-29 16:51:35 +08:00
public ToolURL Open(string url)
2025-06-29 15:52:06 +08:00
{
this.url = url;
return this;
}
2025-06-29 16:51:35 +08:00
public async Task<ToolURL> DownloadAsync(string localPath = null)
2025-06-29 15:52:06 +08:00
{
if (!IsValid)
return this;
if (localPath == null)
{
2025-06-29 16:51:35 +08:00
localPath = Path.Combine(Path.GetTempPath(), GetFilename());
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
try
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
var response = await httpClient.GetAsync(this.url);
if (response.IsSuccessStatusCode)
{
var bytes = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync(localPath, bytes);
}
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
catch
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
// 下载失败
2025-06-29 15:52:06 +08:00
}
return this;
}
2025-06-29 16:51:35 +08:00
public ToolURL Download(string localPath = null)
2025-06-29 15:52:06 +08:00
{
2025-06-29 16:51:35 +08:00
return DownloadAsync(localPath).GetAwaiter().GetResult();
2025-06-29 15:52:06 +08:00
}
2025-06-29 16:51:35 +08:00
#endregion
2025-06-29 15:52:06 +08:00
}
}
2025-06-29 16:51:35 +08:00