惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
量子位
D
DataBreaches.Net
博客园 - 司徒正美
J
Java Code Geeks
博客园 - 【当耐特】
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
B
Blog
The Cloudflare Blog
D
Docker
I
InfoQ
爱范儿
爱范儿
MongoDB | Blog
MongoDB | Blog
腾讯CDC
月光博客
月光博客
Hugging Face - Blog
Hugging Face - Blog
Microsoft Azure Blog
Microsoft Azure Blog
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
小众软件
小众软件
S
SegmentFault 最新的问题
GbyAI
GbyAI
有赞技术团队
有赞技术团队

博客园 - 12不懂3

IOC容器 分布式锁 分布式事务 Myql索引篇+c#性能优化篇 jwt验证机制 .net7.0 .net升级7.0报错1. MySqlConnection is already in use. See https://fl.vu/mysql-conn-reuse 2.SqlSugar.SqlSugarException: Cannot Open when State is Connecting. 3.The current TransactScope is already complete Windos Elastocsearch安装部署汉化 Mysql ElasticSearch C# Common中心目录 ABP.VNET 原书籍《Effective C#:改善C#代码的50个有效方法》 精炼18点 ABP IO读写+Epoll多路复用,计算机底层解析 c# 技术点总结 kuberspere管理工具+k8s安装 K8s核心组件理解 数据库事务和锁
HttpHelper
12不懂3 · 2023-04-12 · via 博客园 - 12不懂3
public class HttpHelpe
{
    public static async Task<T> GetAsync<T>(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
    {
        return await RequestAsync<T>(url, "GET", postData, contentType, timeOut, headers);
    }

    public static async Task<T> PostAsync<T>(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
    {
        return await RequestAsync<T>(url, "POST", postData, contentType, timeOut, headers);
    }

    public static async Task<T> PutAsync<T>(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
    {
        return await RequestAsync<T>(url, "PUT", postData, contentType, timeOut, headers);
    }

    public static async Task<T> DeleteAsync<T>(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
    {
        return await RequestAsync<T>(url, "DELETE", postData, contentType, timeOut, headers);
    }

    private static async Task<T> RequestAsync<T>(string url, string method, string postData = null, string contentType = null, int timeOut = 30,
        Dictionary<string, string> headers = null)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = method;
        request.Timeout = timeOut * 1000;

        if (!string.IsNullOrEmpty(contentType))
        {
            request.ContentType = contentType;
        }

        if (headers != null)
        {
            foreach (var header in headers)
            {
                request.Headers[header.Key] = header.Value;
            }
        }

        if (!string.IsNullOrEmpty(postData) && (method == "POST" || method == "PUT"))
        {
            byte[] dataBytes = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = dataBytes.Length;

            using (Stream stream = await request.GetRequestStreamAsync())
            {
                await stream.WriteAsync(dataBytes, 0, dataBytes.Length);
            }
        }

        using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
        {
            using (Stream stream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(stream);
                string responseBody = await reader.ReadToEndAsync();
                T responseObject = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(responseBody);
                return responseObject;
            }
        }
    }
}