内部服务器上的 Httpclient 请求 returns 超时 c#
Httpclient request on internal server returns timeout c#
我创建了一个与 slack websocket 连接的 windows 服务 (c# .net5)。连接首先转到 slackAPI 以请求 websocket 地址,然后与 slack 服务器建立连接。我写了一个代码来获取那个 websocket 地址,它在我的本地机器上运行良好。但是,当我向内部 windows 服务器 2016 启动我的服务应用程序时,该请求因超时而被取消。
The request was canceled due to the configured HttpClient.Timeout of 60 seconds elapsing.
System.Threading.Tasks.TaskCanceledException: The request was canceled due to the configured HttpClient.Timeout of 60 seconds elapsing.
---> System.TimeoutException: The operation was canceled.
---> System.Threading.Tasks.TaskCanceledException: The operation was canceled.
at System.Net.Http.ConnectHelper.ConnectAsync(Func`3 callback, DnsEndPoint endPoint, HttpRequestMessage requestMessage, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)
at GetWebSocketUrl() in C:\Users\filename.cs:line 130
我尝试将超时时间增加到 5 分钟,但仍然没有成功。
经过一些研究,我发现了这个问题并尝试在我的代码中添加 headers (见下文)但仍然没有。 ()
任何可以帮助我找到解决方案的线索都将不胜感激。
using (HttpClient httpClient = new HttpClient())
{
HttpContent content = new StringContent("", Encoding.UTF8, "application/x-www-form-urlencoded");
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
httpClient.Timeout = TimeSpan.FromMinutes(1);
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko; Google Page Speed Insights) Chrome/27.0.1453 Safari/537.36");
httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/apng,*/*;q=0.8");
httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
httpClient.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.9");
httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
httpClient.DefaultRequestHeaders.Add("Pragma", "no-cache");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", SLACK_TOKEN);
HttpResponseMessage response = await httpClient.PostAsync(SLACK_URI, content);
string bodyResponseString = await response.Content.ReadAsStringAsync();
dynamic jsonBody = JsonConvert.DeserializeObject(bodyResponseString);
if (jsonBody["ok"] == "True")
{
return jsonBody["url"];
}
}
原来是代理问题。只需要在 httpClient 上添加代理就可以了。
IWebProxy _proxyConfig = new WebProxy
{
Address = new Uri($"{proxyIp}:{proxyPort}"),
BypassProxyOnLocal = false,
UseDefaultCredentials = true,
};
HttpClientHandler httpClientHandler = new HttpClientHandler
{
Proxy = _proxyConfig,
};
using (HttpClient _client = new HttpClient(handler: httpClientHandler)) {...}
我创建了一个与 slack websocket 连接的 windows 服务 (c# .net5)。连接首先转到 slackAPI 以请求 websocket 地址,然后与 slack 服务器建立连接。我写了一个代码来获取那个 websocket 地址,它在我的本地机器上运行良好。但是,当我向内部 windows 服务器 2016 启动我的服务应用程序时,该请求因超时而被取消。
The request was canceled due to the configured HttpClient.Timeout of 60 seconds elapsing.
System.Threading.Tasks.TaskCanceledException: The request was canceled due to the configured HttpClient.Timeout of 60 seconds elapsing.
---> System.TimeoutException: The operation was canceled.
---> System.Threading.Tasks.TaskCanceledException: The operation was canceled.
at System.Net.Http.ConnectHelper.ConnectAsync(Func`3 callback, DnsEndPoint endPoint, HttpRequestMessage requestMessage, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)
at GetWebSocketUrl() in C:\Users\filename.cs:line 130
我尝试将超时时间增加到 5 分钟,但仍然没有成功。 经过一些研究,我发现了这个问题并尝试在我的代码中添加 headers (见下文)但仍然没有。 ()
任何可以帮助我找到解决方案的线索都将不胜感激。
using (HttpClient httpClient = new HttpClient())
{
HttpContent content = new StringContent("", Encoding.UTF8, "application/x-www-form-urlencoded");
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
httpClient.Timeout = TimeSpan.FromMinutes(1);
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko; Google Page Speed Insights) Chrome/27.0.1453 Safari/537.36");
httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/apng,*/*;q=0.8");
httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
httpClient.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.9");
httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
httpClient.DefaultRequestHeaders.Add("Pragma", "no-cache");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", SLACK_TOKEN);
HttpResponseMessage response = await httpClient.PostAsync(SLACK_URI, content);
string bodyResponseString = await response.Content.ReadAsStringAsync();
dynamic jsonBody = JsonConvert.DeserializeObject(bodyResponseString);
if (jsonBody["ok"] == "True")
{
return jsonBody["url"];
}
}
原来是代理问题。只需要在 httpClient 上添加代理就可以了。
IWebProxy _proxyConfig = new WebProxy
{
Address = new Uri($"{proxyIp}:{proxyPort}"),
BypassProxyOnLocal = false,
UseDefaultCredentials = true,
};
HttpClientHandler httpClientHandler = new HttpClientHandler
{
Proxy = _proxyConfig,
};
using (HttpClient _client = new HttpClient(handler: httpClientHandler)) {...}