首页 > 其他分享 >正确使用 HttpClient

正确使用 HttpClient

时间:2022-12-06 17:01:18浏览次数:32  
标签:index 正确 HttpClientFactory System private 使用 httpClientCount HttpClient

正确使用 HttpClient

使用 HttpClient 注意事项

  1. HttpClient默认最大并发连接数是2
  2. 本机测试(被请求的WebApi部署在本机)HttpClient不会被限制最大并发连接数
  3. 使用HttpClient要写个工厂类,因为HttpClient不能频繁创建
  4. HttpClient类提供的方法是线程安全的

HttpClient 工厂类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Factory
{
    /// <summary>
    /// HttpClient工厂类
    /// 注意:
    /// 1. HttpClient默认最大并发连接数是2
    /// 2. 本机测试HttpClient不会被限制最大并发连接数
    /// </summary>
    public class HttpClientFactory
    {
        private int _httpClientCount;

        private readonly HttpClient[] _httpClientArray;

        private int _index = -1;

        private readonly object _lock = new object();

        private static readonly HttpClientFactory _instance = new HttpClientFactory();

        public static HttpClientFactory Instance
        {
            get
            {
                return _instance;
            }
        }

        public HttpClientFactory(int httpClientCount = 50)
        {
            _httpClientCount = httpClientCount;
            _httpClientArray = new HttpClient[_httpClientCount];
            for (int i = 0; i < _httpClientCount; i++)
            {
                _httpClientArray[i] = new HttpClient();
            }
        }

        public HttpClient Get()
        {
            lock (_lock)
            {
                _index++;
                if (_index == _httpClientCount)
                {
                    _index = 0;
                }
                return _httpClientArray[_index];
            }
        }
    }
}

标签:index,正确,HttpClientFactory,System,private,使用,httpClientCount,HttpClient
From: https://www.cnblogs.com/s0611163/p/16955790.html

相关文章

  • 使用containerd作为容器运行时拉取镜像的方法
    k8sv1.24版本后默认使用containerd作为容器运行时,很多镜像库使用的是gcr.io,国内可能无法成功拉取。接下来将通过搭建MetricsServer来演示该情况的解决方法。components.......
  • 使用channel控制并发数
    packageconcurrent_testimport("fmt""math/rand""sync""testing""time")funcinit(){rand.Seed(time.Now().UnixNano())}//使......
  • 阻止vue组件vuedraggable在使用时打开浏览器新标签
    前言浏览器在文字拖动时会打开链接,图片拖动时打开新窗口,这是浏览器的特性。vue-draggable组件就是需要拖动的,就与这个特性契合了,但大多时候在项目中我们不需要这个特性。......
  • 正确停止expdp导出任务使用stop_job
    ctr+c进入expdp交互界面输入stop_job=immediateselectjob_name,statefromdba_datapump_jobs;会发现任务已经变为notrunningexpdpnc65/nc65attach=SYS_EXPORT_F......
  • 使用Portableapps.com Launcher制作便携软件 实例教程
    文章搜集于网络,一为分享,二为备忘。使用Portableapps.comLauncher制作便携软件:以EvernotePortable为例PortableApps.comLauncher(以下简称PAL)是PortableApps.com开发的......
  • vue(13)在模板中使用slot插槽排序
    序Slot插槽插槽就是父组件往子组件中插入一些内容。有三种方式,默认插槽,具名插槽,作用域插槽默认插槽就是把父组件中的数据,显示在子组件中,子组件通过一个slot插槽标签显......
  • hutool 农历日期ChineseDate对象的使用
    publicstaticvoidmain(String[]args){//构建农历对象ChineseDatechineseDate=newChineseDate(2022,12,10);//构建公立对象......
  • php的pdo库使用总结
    <?phptry{$dsn="mysql:host=127.0.0.1;port=3306;dbname=test;charset=utf8";$user="root";$password="";#持久连接$pdo=newPDO($ds......
  • python 使用梯度下降法找最小值(Find the minimum using gradient descent)
    最近在看《深度学习全书公式+推导+代码+TensorFlow全程案例》——洪锦魁主编清华大学出版社ISBN978-7-302-61030-4这本书,在第2章神经网络原理中2-3-3偏微分的内......
  • c#使用ShellExecute
    [DllImport("kernel32.dll")] publicstaticexternintWinExec(stringexeName,intoperType);不推荐使用以上代码。ShellExecute若ShellExecute函数调用成功,则返回......