![](/img/trans.png)
[英]How to solve System.Net.Sockets.SocketException on C# Xamarin?
[英]C# HttpClient Multiple Get Requests at the same time giving System.Net.Sockets.SocketException (10060) Exception
正如@John Wu 在 Stackoverflow 上的回答所建议的那样。 我正在使用以下代码在同一主机上进行并发 HTTP Get Requests 以发送 SMS
public async Task SendBulk(List<string> recipentURLs)
{
using (var client = new HttpClient())
{
//Start requests for all of them
var requests = recipentURLs.Select(url => client.GetAsync(url)).ToList();
//Wait for all the requests to finish
await Task.WhenAll(requests);
}
}
现在我有一个例外说
System.Net.Http.HttpRequestException: An error occurred while sending the request.
---> System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond..
---> System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
我发送请求的 API 不需要响应(因为我只使用它发送短信)。
我通过使用 HangFire 在后台工作中使用它。 因此,如果需要很长时间,我可以。
我目前正在寻找的是一种在同一主机上发送多个 HTTP 获取请求的有效方法。
如何安全地处理所有这些并发请求并在其中任何一个失败时重试?
另外我目前正在研究由于 HTTP 节流我不能使用 2 连接到同一主机?
关闭的 HttpClient 实例使 sockets 在 TIME_WAIT state 中打开一小段时间。 如果频繁使用创建和释放 HttpClient 对象的代码路径,应用程序可能会耗尽可用的 sockets。 您应该使用 HttpClientFactory 来避免这种情况。 请参阅使用 ASP.NET Core 中的 IHttpClientFactory 发出 HTTP 请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.