HttpClien Get&Post

    新公司上班第二周,开始进军.Net Core,这方面的东西比较新,所以已经封装好的东西比较少,比如HttpClien之类的开源类库,找了NuGet好久,没有找到,所以先写个简陋的来用着先。

复制代码
using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json;
using System.Net.Http.Headers;

    /// <summary>
    /// Http Method Helper
    /// </summary>
    public static class HttpHelper
    {
        private static HttpClient instance = null;
        public static HttpClient GetClient()
        {
            if (instance == null)
                instance = new HttpClient();
            return instance;
        }

        /// <summary>
        /// Get Method
        /// </summary>
        public static async Task<T> Get<T>(string url)
        {
            try
            {
                var client = GetClient();
                var responseMsg = await client.GetAsync(url);
                if (responseMsg.IsSuccessStatusCode)
                {
                    string strJson = await responseMsg.Content.ReadAsStringAsync();
                    return JsonConvert.DeserializeObject<T>(strJson);
                }
                else
                {
                    return default(T);
                }

            }
            catch
            {
                instance = new HttpClient();
                return default(T);
            }
        }

        /// <summary>
        /// Post Method
        /// </summary>
        public static async Task<T> Post<T>(string url, dynamic para)
        {
            try
            {
                if (para != null)
                {
                    var requestJson = JsonConvert.SerializeObject(para);
                    HttpContent httpContent = new StringContent(requestJson);
                    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    var client = GetClient();

                    var responseJson = await client.PostAsync(url, httpContent).Result.Content.ReadAsStringAsync();
                    return JsonConvert.DeserializeObject<T>(responseJson);
                }
                return default(T);
            }
            catch
            {
                instance = new HttpClient();
                return default(T);
            }
        }
    }
复制代码

 

 调用测试:

//=======================================================
//                  .----.
//               _.'__    `.
//           .--(^)(^^)---/#\
//         .' @          /###\
//         :         ,   #####
//          `-..__.-' _.-\###/
//                `;_:    `"'
//              .'"""""`.
//             /,  ya ,\\
//            //向上吧!409  \\
//            `-._______.-'
//            ___`. | .'___
//           (______|______)
//=======================================================
posted @   山治先生  阅读(667)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示