当前位置:网站首页>[C #] commonly used utils
[C #] commonly used utils
2022-07-18 06:29:00 【Average again】
List of articles
Preface
When we are developing a project, we may encounter some problems to obtain the screen width ,dp px The mutual transformation of , Of course, we can't copy and paste every time we use it . At this time, we need a sharp weapon - Tool class . This tool class contains some of our common methods , In one word, we can get the desired results , It not only simplifies our code, but also saves us valuable time . meanwhile , A good software engineer , Universal tools are his magic weapon to protect himself .( This paragraph is quoted from :Android Project development must -Utils Establishment and use of class )
Code display
Base64 decode :
/// <summary>
/// Base64 decode
/// </summary>
/// <param name="s"> Need to decode string </param>
/// <returns></returns>
public static string Decode(string s)
{
byte[] outputb = Convert.FromBase64String(s);
return Encoding.Default.GetString(outputb);
}
Base64 code :
/// <summary>
/// Base64 code
/// </summary>
/// <param name="s"> Need to encode string </param>
/// <returns></returns>
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);
}
Deep copy :
UserInfo newInfo = JsonSerializer.Deserialize<UserInfo>(JsonSerializer.Serialize(info));
http Tool class :
public class HttpHelper
{
/// <summary>
/// launch POST Synchronization request
/// </summary>
/// <param name="url"></param>
/// <param name="postData"></param>
/// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
/// <param name="headers"> Fill in the message header </param>
/// <returns></returns>
public static string HttpPost(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> 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;
}
}
}
/// <summary>
/// launch POST Asynchronous requests
/// </summary>
/// <param name="url"></param>
/// <param name="postData"></param>
/// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
/// <param name="headers"> Fill in the message header </param>
/// <returns></returns>
public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> 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();
}
}
}
/// <summary>
/// launch GET Synchronization request
/// </summary>
/// <param name="url"></param>
/// <param name="headers"></param>
/// <param name="contentType"></param>
/// <returns></returns>
public static string HttpGet(string url, string contentType = "application/json", Dictionary<string, string> 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;
}
}
/// <summary>
/// launch GET Asynchronous requests
/// </summary>
/// <param name="url"></param>
/// <param name="headers"></param>
/// <param name="contentType"></param>
/// <returns></returns>
public static async Task<string> HttpGetAsync(string url, string contentType = "application/json", Dictionary<string, string> 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();
}
}
/// <summary>
/// launch POST Synchronization request
/// </summary>
/// <param name="url"></param>
/// <param name="postData"></param>
/// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
/// <param name="headers"> Fill in the message header </param>
/// <returns></returns>
public static T HttpPost<T>(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
{
return HttpPost(url, postData, contentType, timeOut, headers).ToEntity<T>();
}
/// <summary>
/// launch POST Asynchronous requests
/// </summary>
/// <param name="url"></param>
/// <param name="postData"></param>
/// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
/// <param name="headers"> Fill in the message header </param>
/// <returns></returns>
public static async Task<T> HttpPostAsync<T>(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
{
var res=await HttpPostAsync(url, postData,contentType, timeOut, headers);
return res.ToEntity<T>();
}
/// <summary>
/// launch GET Synchronization request
/// </summary>
/// <param name="url"></param>
/// <param name="headers"></param>
/// <param name="contentType"></param>
/// <returns></returns>
public static T HttpGet<T>(string url, string contentType = "application/json", Dictionary<string, string> headers = null)
{
return HttpGet(url, contentType, headers).ToEntity<T>();
}
/// <summary>
/// launch GET Asynchronous requests
/// </summary>
/// <param name="url"></param>
/// <param name="headers"></param>
/// <param name="contentType"></param>
/// <returns></returns>
public static async Task<T> HttpGetAsync<T>(string url, string contentType = "application/json", Dictionary<string, string> headers = null)
{
var res= await HttpGetAsync(url, contentType, headers);
return res.ToEntity<T>();
}
}
complete HttpClient encapsulation
边栏推荐
- G1这么强,你确定不了解一下?
- MySQL数据库在触发器中定义游标
- Together with Alibaba cloud, grafana labs will provide the first grafana hosting service in China
- Jerry opened the key pairing, and after the first pairing TWS, it is difficult to pair successfully by cross pairing [article]
- 2、Deep Learning in Higher Dimensions
- 迭代器与生成器
- es5和es6的区别
- 997. 找到小镇的法官
- regular expression
- 論文中的好文佳句摘錄
猜你喜欢

如何將notepad++設置為默認打開方式

杰理之调式时要修改 INI 文件配置【篇】

2、Deep Learning in Higher Dimensions

ECCV 2022 | multi domain long tail distributed learning, research on unbalanced domain generalization (open source)

leetcode:330. 按要求补齐数组

Functions and arrow functions

Iterators and generators

let / const /var的区别

2、Deep Learning in Higher Dimensions

MySQL CREATE TABLE statement error: 1103 incorrect table name
随机推荐
Redis(二)Redis的三种特殊类型
VI editor commands
Notes on Linear Algebra 1
MYSQL建表语句错误:1103-Incorrect table name
如何将notepad++设置为默认打开方式
圖撲 Web 可視化引擎在仿真分析領域的應用
let / const /var的区别
The difference between set and map
Functions and symbols
线性代数 笔记1
个人买股票应该使用什么app更安全,交易速度更快
郑州大学数据库课设资源说明
External interrupt of stm32f4
leetcode:330. 按要求补齐数组
Function advanced application
canal实现从mysql实时同步数据到es
@EqualsAndHashCode注解的使用
R language dplyr package summary_ The all function calculates the mean and median of all numerical data columns in the dataframe data, and filters the numerical data columns with happy (summarize all
杰理之调式时要修改 INI 文件配置【篇】
如何在企业工作中应用知识管理,解决企业的问题?