码迷,mamicode.com
首页 > Web开发 > 详细

http请求端口占用异常

时间:2020-01-16 22:25:17      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:tpm   思路   ensure   stat   http   result   sre   ons   cti   

解决思路:

1.  ESTABLISHED 过多,使用static解决

       static HttpClientHandler StaticHttpClientHandler = new HttpClientHandler
        {
            AllowAutoRedirect = true,
            Proxy = new System.Net.WebProxy(ConfigHelper.FacebookProxyHostUrl)
        };

        static HttpClient StaticHttpClient = new HttpClient(StaticHttpClientHandler, false)
        {
            Timeout = TimeSpan.FromSeconds(10)
        };

        [HttpGet]
        [Route(nameof(TestProxy))]
        public async Task<bool> TestProxy()
        {
            string siteUrl = "https://www.google.com";
            var httpClient = StaticHttpClient;
            var requestMessage = new HttpRequestMessage
            {
                Method = HttpMethod.Get,
                RequestUri = new Uri(siteUrl, UriKind.Absolute),
            };
            var result =await httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead);
            result.EnsureSuccessStatusCode();
            return true;
        }

  

2.方案1导致ESTABLISHED 控制住,但是CLOSE_WAIT太多,使用IHttpClientFactory解决

            //Startup.cs中的public void ConfigureServices(IServiceCollection services)
            services.AddHttpClient();
            services.AddHttpClient("configured-proxy-handler")
            .ConfigurePrimaryHttpMessageHandler(() =>
            {
                return new HttpClientHandler()
                {
                    AllowAutoRedirect = true,
                    Proxy = new System.Net.WebProxy(ConfigHelper.FacebookProxyHostUrl)
                };
            });    


         
        //使用
        private readonly IHttpClientFactory _httpClientFactory;
        public OpsController(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }

        [HttpGet]
        [Route(nameof(TestProxy3))]
        public async Task<bool> TestProxy3()
        {
            string siteUrl = "https://www.google.com";
            var httpClient = _httpClientFactory.CreateClient("configured-proxy-handler");
            httpClient.Timeout = TimeSpan.FromSeconds(10);
            try
            {
                var requestMessage = new HttpRequestMessage
                {
                    Method = HttpMethod.Get,
                    RequestUri = new Uri(siteUrl, UriKind.Absolute),
                };
                var result = await httpClient.GetAsync(siteUrl, HttpCompletionOption.ResponseHeadersRead);
                result.EnsureSuccessStatusCode();
                return true;
            }catch(Exception ex)
            {
                return false;
            }
        }

  

http请求端口占用异常

标签:tpm   思路   ensure   stat   http   result   sre   ons   cti   

原文地址:https://www.cnblogs.com/panpanwelcome/p/12203076.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!