C# .NET Core HttpClient 和 HttpWebRequest 使用

HttpWebRequest

        这是.NET创建者最初开发用于使用HTTP请求的标准类。HttpWebRequest是老版本.net下常用的,较为底层且复杂,访问速度及并发也不甚理想,但是使用HttpWebRequest可以让开发者控制请求/响应流程的各个方面,如 timeouts, cookies, headers, protocols。另一个好处是HttpWebRequest类不会阻塞UI线程。例如,当您从响应很慢的API服务器下载大文件时,您的应用程序的UI不会停止响应。通常和WebResponse一起使用,一个发送请求,一个获取数据。另外HttpWebRequest库已经过时,不适合业务中直接使用,他更适用于框架内部操作。

示例代码:

/// <summary>
        /// HttpWebRequest请求网页示例
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            Stream responseStream = null;
            string url = "https://www.cnblogs.com/";
            try
            {
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);

                //cookie,cookie一般用来验证登录或是跟踪使用
                httpWebRequest.CookieContainer = new CookieContainer();
                httpWebRequest.CookieContainer.Add(new Cookie() { Name = "test", Value = "test1",Domain="www.cnblogs.com" });

                //来源页面
                httpWebRequest.Referer = url;

                //比较重要的UserAgent
                httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0 Gecko/20100101 Firefox/52.0";

                //请求方法,有GET,POPST,PUT等
                httpWebRequest.Method = "GET";

                //如果上传文件,是要设置 GetRequestStream
                //httpWebRequest.GetRequestStream

                try
                {
                    httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                }
                catch (System.Net.WebException we)
                {
                    ///这个说明服务器返回了信息了,不过是非200,301,302这样正常的状态码
                    if (we.Response != null)
                    {
                        httpWebResponse = (HttpWebResponse)we.Response;
                    }
                }

                ///得到返回的stream,如果请求的是一个文件或图片,可以直接使用或保存
                responseStream = httpWebResponse.GetResponseStream();

                ///使用utf8方式读取数据流
                StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);

                ///这里是一次性读取,对于超大的stream,要不断读取并保存
                string html = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
                Console.WriteLine(html.Length);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (httpWebRequest != null) httpWebRequest.Abort();
                if (httpWebResponse != null) httpWebResponse.Close();
                if (responseStream != null) responseStream.Close();
            }
        }

HttpClient

        HttpClient提供强大的功能,提供了异步支持,可以轻松配合async await 实现异步请求,使用HttpClient,在并发量不大的情况,一般没有任何问题;但是在并发量一上去,如果使用不当,会造成很严重的堵塞的情况。

平时我们在使用HttpClient的时候,会将HttpClient包裹在using内部进行声明和初始化,

using(var httpClient = new HttpClient())
{
    //other codes
}

在高并发的情况下,连接来不及释放,socket被耗尽,耗尽之后就会出现喜闻乐见的一个错误:

Unable to connect to the remote serverSystem.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted.

那么如何处理这个问题?“复用HttpClient”即可

        1、HttpClientFacotry很高效,可以最大程度上节省系统socket。(“JUST USE IT AND FXXK SHUT UP”:P)

        2、Factory,顾名思义HttpClientFactory就是HttpClient的工厂,内部已经帮我们处理好了对HttpClient的管理,不需要我们人工进行对象释放,同时,支持自定义请求头,支持DNS更新等等

        从微软源码分析,HttpClient继承自HttpMessageInvoker,而HttpMessageInvoker实质就是HttpClientHandler。

        HttpClientFactory 创建的HttpClient,也即是HttpClientHandler,只是这些个HttpClient被放到了“池子”中,工厂每次在create的时候会自动判断是新建还是复用。(默认生命周期为2min)

还理解不了的话,可以参考Task和Thread的关系,解决方案请看下面HttpClientFactory示例。

IHttpClientFactory

一、可以参考微软官方提供的方法:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1

https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/http-requests?view=aspnetcore-5.0

二、我的解决方案是根据官方提供的方法,选择一种最适合项目的写法进行改造。

        1、nuget添加包Microsoft.AspNetCore.Http;

        2、startup里ConfigureServices方法添加代码:

services.AddHttpClient();

或者

public void ConfigureServices(IServiceCollection services)
        {
            //other codes
            
            services.AddHttpClient("client_1",config=>  //这里指定的name=client_1,可以方便我们后期服用该实例 比如已经填写url和header
            {
                config.BaseAddress= new Uri("http://client_1.com");
                config.DefaultRequestHeaders.Add("header_1","header_1");            });

            services.AddHttpClient();

            //other codes
            services.AddMvc().AddFluentValidation();
        }

        3、可以使用依赖项注入 (DI) 来请求 IHttpClientFactory。 以下代码使用 IHttpClientFactory 来创建 HttpClient 实例:(官方demo)

public class BasicUsageModel : PageModel
{
    private readonly IHttpClientFactory _clientFactory;

    public IEnumerable<GitHubBranch> Branches { get; private set; }

    public bool GetBranchesError { get; private set; }

    public BasicUsageModel(IHttpClientFactory clientFactory)
    {
        _clientFactory = clientFactory;
    }

    public async Task OnGet()
    {
        var request = new HttpRequestMessage(HttpMethod.Get,
            "https://api.github.com/repos/aspnet/AspNetCore.Docs/branches");
        request.Headers.Add("Accept", "application/vnd.github.v3+json");
        request.Headers.Add("User-Agent", "HttpClientFactory-Sample");

        var client = _clientFactory.CreateClient();

        var response = await client.SendAsync(request);

        if (response.IsSuccessStatusCode)
        {
            using var responseStream = await response.Content.ReadAsStreamAsync();
            Branches = await JsonSerializer.DeserializeAsync
                <IEnumerable<GitHubBranch>>(responseStream);
        }
        else
        {
            GetBranchesError = true;
            Branches = Array.Empty<GitHubBranch>();
        }
    }
}

在实际使用中,我们经常会用NewtonJson序列化,给一个简单的Demo: 

string api_domain = _config.GetSection("OuterApi:open-api").Value;
                string api_url = $"{api_domain}/common-service/api/basic?code={code}";
                var request = new HttpRequestMessage(HttpMethod.Get, api_url);
                request.Headers.Add("Accept", "application/vnd.github.v3+json");


                var client = _clientFactory.CreateClient();

                var response = await client.SendAsync(request);

                Result<List<OpenApiDictModel>> apiRet = new Result<List<OpenApiDictModel>>();
                if (response.IsSuccessStatusCode)
                {
                    string responseStr = await response.Content.ReadAsStringAsync();
                    apiRet = JsonConvert.DeserializeObject<Result<List<OpenApiDictModel>>>(responseStr);
                }

 IHttpClientFactory帮助类

using ICSharpCode.SharpZipLib.GZip;
using Jareds.Common.Logger;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace ZYS.MessageCenter.Facade.Common
{
    /// <summary>
    /// http 请求服务
    /// </summary>
    public interface IHttpClientHelper
    {
        /// <summary>
        /// 使用post返回异步请求直接返回对象
        /// </summary>
        /// <typeparam name="T">返回对象类型</typeparam>
        /// <typeparam name="T2">请求对象类型</typeparam>
        /// <param name="url">请求链接</param>
        /// <param name="obj">请求对象数据</param>
        /// <param name="header">请求头</param>
        /// <param name="postFrom">表单提交 注* postFrom不为null 代表表单提交, 为null标识惊悚格式请求</param>
        /// <param name="gzip">是否压缩</param>
        /// <returns>请求返回的目标对象</returns>
        Task<T> PostObjectAsync<T, T2>(string url, T2 obj, Dictionary<string, string> header = null, Dictionary<string, string> postFrom = null, bool gzip = false);
        /// <summary>
        /// 使用Get返回异步请求直接返回对象
        /// </summary>
        /// <typeparam name="T">请求对象类型</typeparam>
        /// <param name="url">请求链接</param>
        /// <returns>返回请求的对象</returns>
        Task<T> GetObjectAsync<T>(string url);
    }

    /// <summary>
    /// http 请求服务
    /// </summary>
    public class HttpClientHelper : IHttpClientHelper
    {
        private readonly IHttpClientFactory _httpClientFactory;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="httpClientFactory"></param>
        public HttpClientHelper(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }
        #region http 请求方式
        /// <summary>
        /// 使用post方法异步请求 
        /// </summary>
        /// <param name="url">目标链接</param>
        /// <param name="posData">发送的参数JSON字符串</param>
        /// <param name="header">请求头</param>
        /// <param name="posFrom">表单提交格式</param>
        /// <param name="gzip">是否压缩</param>
        /// <returns>返回的字符串</returns>
        public async Task<string> PostAsync(string url, string posData, Dictionary<string, string> header = null, Dictionary<string, string> posFrom = null, bool gzip = false)
        {

            //从工厂获取请求对象
            var client = _httpClientFactory.CreateClient();
            //消息状态
            string responseBody = string.Empty;
            //存在则是表单提交信息
            if (posFrom != null)
            {
                var formData = new MultipartFormDataContent();
                foreach (var item in posFrom)
                {
                    formData.Add(new StringContent(item.Value), item.Key);
                }
                //提交信息
                var result = await client.PostAsync(url, formData);
                if (!result.IsSuccessStatusCode)
                {
                    Log.Error("请求出错");
                    return null;
                }
                //获取消息状态
                responseBody = await result.Content.ReadAsStringAsync();
            }
            else
            {
                HttpContent content = new StringContent(posData);
                if (header != null)
                {
                    client.DefaultRequestHeaders.Clear();
                    foreach (var item in header)
                    {
                        client.DefaultRequestHeaders.Add(item.Key, item.Value);
                    }
                }
                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                HttpResponseMessage response = await client.PostAsync(url, content);
                if (!response.IsSuccessStatusCode)
                {
                    Log.Error("请求出错");
                    return null;
                }

                //response.EnsureSuccessStatusCode();
                if (gzip)
                {
                    GZipInputStream inputStream = new GZipInputStream(await response.Content.ReadAsStreamAsync());
                    responseBody = new StreamReader(inputStream).ReadToEnd();
                }
                else
                {
                    responseBody = await response.Content.ReadAsStringAsync();

                }
            }

            return responseBody;

        }

        /// <summary>
        /// 使用get方法异步请求
        /// </summary>
        /// <param name="url">目标链接</param>
        /// <param name="header"></param>
        /// <param name="Gzip"></param>
        /// <returns>返回的字符串</returns>
        public async Task<string> GetAsync(string url, Dictionary<string, string> header = null, bool Gzip = false)
        {
            var client = _httpClientFactory.CreateClient();
            //HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false });
            if (header != null)
            {
                client.DefaultRequestHeaders.Clear();
                foreach (var item in header)
                {
                    client.DefaultRequestHeaders.Add(item.Key, item.Value);
                }
            }
            HttpResponseMessage response = await client.GetAsync(url);
            if (!response.IsSuccessStatusCode)
            {
                Log.Error("请求出错");
                return null;
            }

            //response.EnsureSuccessStatusCode();//用来抛异常
            string responseBody = "";
            if (Gzip)
            {
                GZipInputStream inputStream = new GZipInputStream(await response.Content.ReadAsStreamAsync());
                responseBody = new StreamReader(inputStream).ReadToEnd();
            }
            else
            {
                responseBody = await response.Content.ReadAsStringAsync();

            }
            return responseBody;
        }

        /// <summary>
        /// 使用post返回异步请求直接返回对象
        /// </summary>
        /// <typeparam name="T">返回对象类型</typeparam>
        /// <typeparam name="T2">请求对象类型</typeparam>
        /// <param name="url">请求链接</param>
        /// <param name="obj">请求对象数据</param>
        /// <param name="header">请求头</param>
        /// <param name="postFrom">表单提交 表单提交 注* postFrom不为null 代表表单提交, 为null标识惊悚格式请求</param>
        /// <param name="gzip">是否压缩</param>
        /// <returns>请求返回的目标对象</returns>
        public async Task<T> PostObjectAsync<T, T2>(string url, T2 obj, Dictionary<string, string> header = null, Dictionary<string, string> postFrom = null, bool gzip = false)
        {
            String json = JsonConvert.SerializeObject(obj);
            string responseBody = await PostAsync(url, json, header, postFrom, gzip); //请求当前账户的信息
            if (responseBody is null)
            {
                return default(T);
            }
            return JsonConvert.DeserializeObject<T>(responseBody);//把收到的字符串序列化
        }

        /// <summary>
        /// 使用Get返回异步请求直接返回对象
        /// </summary>
        /// <typeparam name="T">请求对象类型</typeparam>
        /// <param name="url">请求链接</param>
        /// <returns>返回请求的对象</returns>
        public async Task<T> GetObjectAsync<T>(string url)
        {
            string responseBody = await GetAsync(url); //请求当前账户的信息
            if (responseBody is null)
            {
                return default(T);
            }
            return JsonConvert.DeserializeObject<T>(responseBody);//把收到的字符串序列化
        }

        #endregion

    }
}

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/28244.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

run方法执行过程分析

文章目录 run方法核心流程SpringApplicationRunListener监听器监听器的配置与加载SpringApplicationRunListener源码解析实现类EventPublishingRunListener 初始化ApplicationArguments初始化ConfigurableEnvironment获取或创建环境配置环境 打印BannerSpring应用上下文的创建S…

1.从0搭建前端Vue项目工程

我们通过vue官方提供的脚手架Vue-cli来快速生成一个Vue的项目模板。 **注意&#xff1a;**需要先安装NodeJS&#xff0c;然后才能安装Vue-cli。 环境准备好了&#xff0c;接下来我们需要通过Vue-cli创建一个vue项目&#xff0c;然后再学习一下vue项目的目录结构。Vue-cli提供了…

C语言学习笔记-初阶(27)操作符详解1:位操作

1. 操作符的分类 上述的操作符&#xff0c;我们已经学过算术操作符、赋值操作符、逻辑操作符、条件操作符和部分的单目操作符&#xff0c;今天继续介绍⼀部分&#xff0c;操作符中有一些操作符和二进制有关系&#xff0c;我们先铺垫一下二进制的和进制转换的知识。 2. 二进制、…

蓝桥杯备考:动态规划线性dp之传球游戏

按照动态规划的做题顺序 step1&#xff1a;定义状态表示 f[i][j] 表示 第i次传递给了第j号时一共有多少种方案 step2: 推到状压公式 step3:初始化 step4:最终结果实际上就是f[m][1] #include <iostream> #include <cstring> using namespace std;const int N …

FinRobot:一个使用大型语言模型进行金融分析的开源AI代理平台

文章目录 前言一、生态系统1. 金融AI代理&#xff08;Financial AI Agents&#xff09;2. 金融大型语言模型&#xff08;Financial LLMs&#xff09;3. LLMOps4. 数据操作&#xff08;DataOps&#xff09;5. 多源LLM基础模型&#xff08;Multi-Source LLM Foundation Models&am…

基于Windows11的RAGFlow安装方法简介

基于Windows11的RAGFlow安装方法简介 一、下载安装Docker docker 下载地址 https://www.docker.com/ Download Docker Desktop 选择Download for Winodws AMD64下载Docker Desktop Installer.exe 双点击 Docker Desktop Installer.exe 进行安装 测试Docker安装是否成功&#…

uniapp 常用 UI 组件库

1. uView UI 特点&#xff1a; 组件丰富&#xff1a;提供覆盖按钮、表单、图标、表格、导航、图表等场景的内置组件。跨平台支持&#xff1a;兼容 App、H5、小程序等多端。高度可定制&#xff1a;支持主题定制&#xff0c;组件样式灵活。实用工具类&#xff1a;提供时间、数组操…

【四.RAG技术与应用】【12.阿里云百炼应用(下):RAG的云端优化与扩展】

在上一篇文章中,我们聊了如何通过阿里云百炼平台快速搭建一个RAG(检索增强生成)应用,实现文档智能问答、知识库管理等基础能力。今天咱们继续深入,聚焦两个核心问题:如何通过云端技术优化RAG的效果,以及如何扩展RAG的应用边界。文章会穿插实战案例,手把手带你踩坑避雷。…

LabVIEW虚拟频谱分析仪

在电子技术快速发展的今天&#xff0c;频谱分析已成为信号优化与故障诊断的核心手段。传统频谱分析仪虽功能强大&#xff0c;但价格高昂且体积笨重&#xff0c;难以满足现场调试或移动场景的需求。 基于LabVIEW开发的虚拟频谱分析仪通过软件替代硬件功能&#xff0c;显著降低成…

解决各大浏览器中http地址无权限调用麦克风摄像头问题(包括谷歌,Edge,360,火狐)后续会陆续补充

项目场景&#xff1a; 在各大浏览器中http地址调用电脑麦克风摄像头会没有权限&#xff0c;http协议无法使用多媒体设备 原因分析&#xff1a; 为了用户的隐私安全&#xff0c;http协议无法使用多媒体设备。因为像摄像头和麦克风属于可能涉及重大隐私问题的API&#xff0c;ge…

知识图谱科研文献推荐系统vue+django+Neo4j的知识图谱

文章结尾部分有CSDN官方提供的学长 联系方式名片 文章结尾部分有CSDN官方提供的学长 联系方式名片 关注B站&#xff0c;有好处&#xff01; &#x1f4d1; 编号&#xff1a;D030 &#x1f4d1; vuedjangoneo4jmysql 前后端分离架构、图数据库 &#x1f4d1; 文献知识图谱&#…

NModbus 连接到Modbus服务器(Modbus TCP)

1、在项目中通过NuGet添加NModbus&#xff0c;在界面中添加一个Button。 using NModbus.Device; using NModbus; using System.Net.Sockets; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Docu…

Ubuntu问题 - 在ubuntu上使用 telnet 测试远程的IP:端口是否连通

使用 telnet 测试端口连接 安装 telnet&#xff08;如果尚未安装&#xff09;&#xff1a; sudo apt update sudo apt install telnet使用 telnet 测试端口&#xff1a; 假设你要测试 example.com 的 80 端口&#xff08;HTTP&#xff09;&#xff0c;可以运行以下命令&#xf…

全网最全!解决VirtualBox或VMware启动虚拟机时报错问题“不能为虚拟电脑打开一个新任务”和“Error In suplibOslnit”解决方案超全超详细

我自己下载并配置完VritualBox和OpenEuler之后帮助了几个朋友和我的室友在她们的电脑上下载安装时出现了不同的问题&#xff0c;下面我将简单解释一下如何解决配置时出现的两个无法启动虚拟器的问题。 目录 问题&#xff1a;“不能为虚拟电脑XX打开一个新任务”和“Error In …

SpringMVC(2)传递JSON、 从url中获取参数、上传文件、cookie 、session

Spring 事务传播机制包含以下 7 种&#xff1a; Propagation.REQUIRED&#xff1a;默认的事务传播级别&#xff0c;它表示如果当前存在事务&#xff0c;则加入该事务&#xff1b;如果当前没有事务&#xff0c;则创建一个新的事务。Propagation.SUPPORTS&#xff1a;如果当前存…

软考中级-数据库-3.4 数据结构-图

图的定义 一个图G(Graph)是由两个集合:V和E所组成的&#xff0c;V是有限的非空顶点(Vertex)集合&#xff0c;E是用顶点表示的边(Edge)集合&#xff0c;图G的顶点集和边集分别记为V(G)和E(G)&#xff0c;而将图G记作G(V&#xff0c;E)。可以看出&#xff0c;一个顶点集合与连接这…

开源表单、投票、测评平台部署教程

填鸭表单联合宝塔面板深度定制,自宝塔面板 9.2 版本开始,在宝塔面板-软件商店中可以一键部署填鸭表单系统。 简单操作即可拥有属于自己的表单问卷系统,快速赋能业务。即使小白用户也能轻松上手。 社区版体验地址:https://demo.tduckapp.com/home 前端项目地址: tduck-fro…

IDEA 接入 Deepseek

在本篇文章中&#xff0c;我们将详细介绍如何在 JetBrains IDEA 中使用 Continue 插件接入 DeepSeek&#xff0c;让你的 AI 编程助手更智能&#xff0c;提高开发效率。 一、前置准备 在开始之前&#xff0c;请确保你已经具备以下条件&#xff1a; 安装了 JetBrains IDEA&…

Metal学习笔记七:片元函数

知道如何通过将顶点数据发送到 vertex 函数来渲染三角形、线条和点是一项非常巧妙的技能 — 尤其是因为您能够使用简单的单行片段函数为形状着色。但是&#xff0c;片段着色器能够执行更多操作。 ➤ 打开网站 https://shadertoy.com&#xff0c;在那里您会发现大量令人眼花缭乱…

深入浅出 Go 语言:协程(Goroutine)详解

深入浅出 Go 语言&#xff1a;协程(Goroutine)详解 引言 Go 语言的协程&#xff08;goroutine&#xff09;是其并发模型的核心特性之一。协程允许你轻松地编写并发代码&#xff0c;而不需要复杂的线程管理和锁机制。通过协程&#xff0c;你可以同时执行多个任务&#xff0c;并…