【C#】常用的Utils

文章目录

    • 前言
    • 代码展示
    • 造轮子
    • 完整HttpClient封装

前言

工欲善其事必先利其器!

我们在进行项目开发的时候可能会遇到一些获取屏幕宽度,dp px的相互转换等问题,我们当然不能每用一次就复制粘贴一次。这时候就需要一个利器-工具类。 这个工具类包含了我们一些公用的方法,只需要一句话我们就可以拿到想要的结果,既精简了我们的代码又省下了我们宝贵的时间。同时,一个好的软件工程师,万能的工具类便是他的护身法宝。(本段引用自:Android 项目开发必备-Utils类的建立与使用)

代码展示

Base64解码:

        /// 
        /// Base64解码
        /// 
        /// 需解码字符串
        /// 
        public static string Decode(string s)
        {
            byte[] outputb = Convert.FromBase64String(s);
            return Encoding.Default.GetString(outputb);
        }

Base64编码:

        /// 
        /// Base64编码
        /// 
        /// 需编码字符串
        /// 
        public static string EncodeByUTF8(string s)
        {
            System.Text.Encoding encode = System.Text.Encoding.UTF8;
            byte[] bytedata = encode.GetBytes(s);
            return Convert.ToBase64String(bytedata, 0, bytedata.Length);
        }

深拷贝:

UserInfo newInfo = JsonSerializer.Deserialize(JsonSerializer.Serialize(info));

http工具类:

 public class HttpHelper
    {
        /// 
        /// 发起POST同步请求
        /// 
        /// 
        /// 
        /// application/xml、application/json、application/text、application/x-www-form-urlencoded
        /// 填充消息头        
        /// 
        public static string HttpPost(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary headers = null)
        {
            postData = postData ?? "";
            using (HttpClient client = new HttpClient())
            {
                if (headers != null)
                {
                    foreach (var header in headers)
                        client.DefaultRequestHeaders.Add(header.Key, header.Value);
                }
                using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
                {
                    if (contentType != null)
                        httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
 
                    HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
                    return response.Content.ReadAsStringAsync().Result;
                }
            }
        }
 
 
        /// 
        /// 发起POST异步请求
        /// 
        /// 
        /// 
        /// application/xml、application/json、application/text、application/x-www-form-urlencoded
        /// 填充消息头        
        /// 
        public static async Task HttpPostAsync(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary headers = null)
        {
            postData = postData ?? "";
            using (HttpClient client = new HttpClient())
            {
                client.Timeout = new TimeSpan(0, 0, timeOut);
                if (headers != null)
                {
                    foreach (var header in headers)
                        client.DefaultRequestHeaders.Add(header.Key, header.Value);
                }
                using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
                {
                    if (contentType != null)
                        httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
 
                    HttpResponseMessage response = await client.PostAsync(url, httpContent);
                    return await response.Content.ReadAsStringAsync();
                }
            }
        }
 
        /// 
        /// 发起GET同步请求
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string HttpGet(string url, string contentType = "application/json", Dictionary headers = null)
        {
            using (HttpClient client = new HttpClient())
            {
                if (contentType != null)
                    client.DefaultRequestHeaders.Add("ContentType", contentType);
                if (headers != null)
                {
                    foreach (var header in headers)
                        client.DefaultRequestHeaders.Add(header.Key, header.Value);
                }
                HttpResponseMessage response = client.GetAsync(url).Result;
                return response.Content.ReadAsStringAsync().Result;
            }
        }
 
        /// 
        /// 发起GET异步请求
        /// 
        /// 
        /// 
        /// 
        /// 
        public static async Task HttpGetAsync(string url, string contentType = "application/json", Dictionary headers = null)
        {
            using (HttpClient client = new HttpClient())
            {
                if (contentType != null)
                    client.DefaultRequestHeaders.Add("ContentType", contentType);
                if (headers != null)
                {
                    foreach (var header in headers)
                        client.DefaultRequestHeaders.Add(header.Key, header.Value);
                }
                HttpResponseMessage response = await client.GetAsync(url);
                return await response.Content.ReadAsStringAsync();
            }
        }
 
 
 
        /// 
        /// 发起POST同步请求
        /// 
        /// 
        /// 
        /// application/xml、application/json、application/text、application/x-www-form-urlencoded
        /// 填充消息头        
        /// 
        public static T HttpPost(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary headers = null)
        {
            return HttpPost(url, postData, contentType, timeOut, headers).ToEntity();
        }
 
 
        /// 
        /// 发起POST异步请求
        /// 
        /// 
        /// 
        /// application/xml、application/json、application/text、application/x-www-form-urlencoded
        /// 填充消息头        
        /// 
        public static async Task HttpPostAsync(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary headers = null)
        {
            var res=await HttpPostAsync(url, postData,contentType, timeOut, headers);
            return res.ToEntity();
        }
 
        /// 
        /// 发起GET同步请求
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T HttpGet(string url, string contentType = "application/json", Dictionary headers = null)
        {
            return HttpGet(url, contentType, headers).ToEntity();
        }
 
        /// 
        /// 发起GET异步请求
        /// 
        /// 
        /// 
        /// 
        /// 
        public static async Task HttpGetAsync(string url, string contentType = "application/json", Dictionary headers = null)
        {
            var res= await HttpGetAsync(url, contentType, headers);
            return res.ToEntity();
        }
    }

造轮子

显然,车轮子是圆形的,这是大家公认的,最合适的形状。而你非要发明另一种形状的轮子,这种行为就叫「重复发明轮子(Reinventing the wheel)」,即「造轮子」—— 明知道你做的不可能比前辈做得更好,却仍然坚持要做。

放到编程中,就是说业界已经有公认的软件或者库了,你明知道自己不可能比它做得更好,却还坚持要做。作为练习,造轮子可以增加自己的经验,很多事情看起来简单,但只有自己动手,才会发现其中的难点。当然实际开发中也有很多情况不得不造轮子,比如希望做到「自主知识产权」、刷 KPI 之类的;或者造轮子的人真的觉得自己开发的版本有更强的功能、更好的性能也说不定呢。

重复制造它的意义有两个
1:你的到了锻炼
2:你牛逼的话可以比前人造的更好,间接造福了全世界

重复制造轮子和重复发明轮子是不一样的。发明是researching,制造是engineering,不能混为一谈。我们说,不要重复发明轮子,但是没有说,不要重复制造轮子。

–引自程序员口中的“造轮子”是什么意思?

完整HttpClient封装

(笔记)C# HttpClient封装

你可能感兴趣的:(#,WEB_C#,开发,c#,android,java)