http请求端口占用异常
Posted panpanwelcome
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了http请求端口占用异常相关的知识,希望对你有一定的参考价值。
解决思路:
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请求端口占用异常的主要内容,如果未能解决你的问题,请参考以下文章
Intellij 代理抛出异常错误: java.rmi.server.ExportException: Port already in use: 1099,端口被占用