C# 应用 - 使用 HttpClient 发起 Http 请求

1. 需要的库类

\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Net.Http.dll
    System.Net.Http.HttpClient

2. 代码

public class HttpClientHelper
{
    private static readonly object LockObj = new object();
    private static HttpClient client = null;
    public HttpClientHelper() 
    {
        GetInstance();
    }
    
    public static HttpClient GetInstance()
    {
        if (client == null)
        {
            lock (LockObj)
            {
                if (client == null)
                {
                    client = new HttpClient();
                }
            }
        }
        return client;
    }
    
    public async Task<string> PostAsync(string url, string strJson)//post异步请求方法
    {
        try
        {
            HttpContent content = new StringContent(strJson);
            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            //由HttpClient发出异步Post请求
            HttpResponseMessage res = await client.PostAsync(url, content);
            if (res.StatusCode == System.Net.HttpStatusCode.OK)
            {
                string str = res.Content.ReadAsStringAsync().Result;
                return str;
            }
            else
                return null;
        }
        catch (Exception ex)
        {
            return null;
        }
    }
 
    public string Post(string url, string strJson)//post同步请求方法
    {
        try
        {
            HttpContent content = new StringContent(strJson);
            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            //client.DefaultRequestHeaders.Connection.Add("keep-alive");
            //由HttpClient发出Post请求
            Task<HttpResponseMessage> res = client.PostAsync(url, content);
            if (res.Result.StatusCode == System.Net.HttpStatusCode.OK)
            {
                string str = res.Result.Content.ReadAsStringAsync().Result;
                return str;
            }
            else
                return null;
        }
        catch (Exception ex)
        {
            return null;
        }
    }
 
    public string Get(string url)
    {
        try
        {
            var responseString = client.GetStringAsync(url);
            return responseString.Result;
        }
        catch (Exception ex)
        {
            return null;
        }
    }
}

3. Http 系列

3.1 发起请求

使用 HttpWebRequest 发起 Http 请求:https://www.cnblogs.com/MichaelLoveSna/p/14501036.html
使用 WebClient 发起 Http 请求 :https://www.cnblogs.com/MichaelLoveSna/p/14501582.html
使用 HttpClient 发起 Http 请求:https://www.cnblogs.com/MichaelLoveSna/p/14501592.html
使用 HttpClient 发起上传文件、下载文件请求:https://www.cnblogs.com/MichaelLoveSna/p/14501603.html

3.2 接受请求

使用 HttpListener 接受 Http 请求:https://www.cnblogs.com/MichaelLoveSna/p/14501628.html
使用 WepApp 接受 Http 请求:https://www.cnblogs.com/MichaelLoveSna/p/14501612.html
使用 WepApp 处理文件上传、下载请求:https://www.cnblogs.com/MichaelLoveSna/p/14501616.html

上一篇:C# 应用 - 使用 HttpClient 发起上传文件、下载文件请求


下一篇:记录core中GRPC长连接导致负载均衡不均衡问题一:查看源码,看创建过程