问题描述
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(Url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
using (var task = client.PostAsJsonAsync(Url, body))
{
if (task.Result.StatusCode != HttpStatusCode.OK)
throw new Exception(task.Result.ReasonPhrase);
}
}
不确定为什么我们通常只能允许每个套接字地址(协议/网络地址/端口)使用一次xx.xxx.xxx.xx:80错误
System.AggregateException: One or more errors occurred. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted xx.xx.xx.xx:80
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
有问题的错误是WSAEADDRINUSE
(10048):
这意味着您或者有多个HttpClient
对象试图同时将自己绑定到相同的本地IP/端口,或者另一个应用程序正在使用HttpClient
试图同时使用的IP/端口. /p>
更有可能的是,您可能过于频繁地发布HTTP请求,并且可能没有完全占用响应,这将阻止ASP池化和重用连接,从而随着时间的推移遇到端口耗尽的情况.
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(Url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
using (var task = client.PostAsJsonAsync(Url, body))
{
if (task.Result.StatusCode != HttpStatusCode.OK)
throw new Exception(task.Result.ReasonPhrase);
}
}
Not Sure why we get the Only one usage of each socket address (protocol/network address/port) is normally permitted xx.xxx.xxx.xx:80 error
System.AggregateException: One or more errors occurred. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted xx.xx.xx.xx:80
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
The error in question is WSAEADDRINUSE
(10048):
Which means you either have multiple HttpClient
objects trying to bind themselves to the same local IP/Port at the same time, or another app is using an IP/Port that an HttpClient
is trying to also use.
More likely, you are probably posting HTTP requests too often, and maybe not fully consuming the responses, which would prevent ASP from pooling and reusing connections and thus encountering port exhaustion over time.
这篇关于HttpClient:通常只允许每个套接字地址(协议/网络地址/端口)使用一种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!