using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Competition.Common {
public static class HttpHelper {
public static async Task<(bool, string)> GetAsync<T>(string url, T model) where T : class, new()
=> await GetAsync(url, DictToUrl<T>(model));
public static async Task<(bool, TResult)> GetAsync<T, TResult>(string url, T model) where T : class, new() where TResult : new()
=> await GetAsync<TResult>(url, DictToUrl<T>(model));
public static async Task<(bool, string)> GetAsync(string url, string paramsData) {
return await CallGetResultAsync(url, paramsData, (content) => content);
}
public static async Task<(bool, T)> GetAsync<T>(string url, string paramsData) where T : new() {
return await CallGetResultAsync(url, paramsData, (content) => JObject.Parse(content).ToObject<T>());
}
private static async Task<(bool, T)> CallGetResultAsync<T>(string url, string paramsData, Func<string, T> func) {
url = url + (paramsData.StartsWith("?") ? "" : "?") + paramsData;
using (var client = new HttpClient()) {
using (var response = await client.GetAsync(url)) {
if (response.IsSuccessStatusCode) {
var resStr = await response.Content.ReadAsStringAsync();
if (resStr != null && func != null) {
var da = func.Invoke(resStr);
return da != null ? (true, da) : (false, default);
}
}
return (false, default);
}
}
}
public static async Task<(bool isTrue, TResult res)> Post_JsonAsnyc<TResult>(string url, string postData) where TResult : new() {
return await PostAsnyc<TResult>(url, postData, "application/json");
}
public static async Task<(bool, string)> Post_JsonAsnyc(string url, string postData) {
return await PostAsnyc(url, postData, "application/json");
}
public static async Task<(bool, string)> Post_JsonAsnyc<T1>(string url, T1 postData, Dictionary<string, string> headers = null) where T1 : new() {
return await PostAsnyc(url, JsonConvert.SerializeObject(postData), "application/json", headers);
}
public static async Task<(bool, string)> Post_OctetStreamAsnyc(string url, string postData) {
return await PostAsnyc(url, postData, "application/octet-stream");
}
private static async Task<(bool isTrue, string resStr)> PostAsnyc(string url, string postData, string contentType = "application/x-www-form-urlencoded", Dictionary<string, string> headers = null) {
return await CallPostResultAsync(url, postData, contentType, (content) => content, headers);
}
private static async Task<(bool isTrue, T res)> PostAsnyc<T>(string url, string postData, string contentType = "application/x-www-form-urlencoded") where T : new() {
return await CallPostResultAsync(url, postData, contentType, (content) => JsonConvert.DeserializeObject<T>(content));
}
private static async Task<(bool, T)> CallPostResultAsync<T>(string url, string postData, string contentType, Func<string, T> func, Dictionary<string, string> headers = null) {
HttpContent httpContent = new StringContent(postData, Encoding.UTF8);
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
using (var client = new HttpClient()) {
if (headers != null) {
foreach (var header in headers)
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
using (var response = await client.PostAsync(url, httpContent)) {
if (response.IsSuccessStatusCode) {
var resStr = await response.Content.ReadAsStringAsync();
if (resStr != null && func != null) {
var da = func.Invoke(resStr);
return da != null ? (true, da) : (false, default);
}
}
return (false, default);
}
}
}
public static string DictToUrl(Dictionary<string, string> dict) {
StringBuilder sb = new StringBuilder();
foreach (var item in dict) {
sb.Append(item.Key + "=" + item.Value + "&");
}
return sb.ToString();
}
public static string DictToUrl<T>(T t) where T : class, new() {
if (t == default(T)) return "";
Type obj = typeof(T);
PropertyInfo[] pArray = obj.GetProperties();
StringBuilder sb = new StringBuilder();
Array.ForEach<PropertyInfo>(pArray, p => {
var val = p.GetValue(t, null);
if (val != null) {
var jsonPro = p.GetCustomAttribute<JsonPropertyAttribute>();
if (jsonPro != null && !string.IsNullOrEmpty(jsonPro.PropertyName)) {
sb.Append(jsonPro.PropertyName + "=" + val + "&");
}
else {
sb.Append(p.Name + "=" + val + "&");
}
}
});
return sb.ToString();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!