HttpClien Get&Post

Posted 山治先生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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  \\\\
//            `-._______.-\'
//            ___`. | .\'___
//           (______|______)
//=======================================================

以上是关于HttpClien Get&Post的主要内容,如果未能解决你的问题,请参考以下文章

HttpClient请求URL

Django + Axios & Ajax post和get 传参

GET&&POST请求编码过程

POST &amp; GET &amp; Ajax 全解

php判断请求类型(ajax|get|post|cli)

Ajax 发送 GET 而不是 POST