c# - 消息为 "The request was aborted: The request was canceled."的 WebException 的根本原因是什么

标签 c# .net httpclient system.net

我正在使用 HttpClient 发出 Http 请求。我偶尔会看到以下错误:

Exception Type: System.Net.WebException
Error message: The request was aborted: The request was canceled.
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
Exception Type: System.Net.Http.HttpRequestException
Error message: An error occurred while sending the request.

我正在尝试了解根本原因可能是什么。我可以在 StackOverflow 上找到很多关于此消息的帖子(例如 this post ),但所有帖子似乎都与寻找解决方法有关(通常是将 KeepAlive 设置为 false 之类的事情,我不想要),而我只是想了解错误的真正含义,以便我可以决定是否采取措施以及在何处采取措施。

最佳答案

没有人能确定。

启用 network client trace可以帮助检测根本原因。

注意:

文档中定义了5个跟踪源,但是在System.Net的Logging类中有6个:

s_WebTraceSource = new NclTraceSource("System.Net");
s_HttpListenerTraceSource = new NclTraceSource("System.Net.HttpListener"); // not in the documentation
s_SocketsTraceSource = new NclTraceSource("System.Net.Sockets");
s_WebSocketsTraceSource = new NclTraceSource("System.Net.WebSockets");
s_CacheTraceSource = new NclTraceSource("System.Net.Cache");
s_TraceSourceHttpName = new NclTraceSource("System.Net.Http");

启用所有 5 个时的示例日志:

System.Net Verbose: 0 : [9520] WebRequest::Create(http://localhost:9876/)
System.Net Verbose: 0 : [9520] HttpWebRequest#58870012::HttpWebRequest(http://localhost:9876/#363619410)
System.Net Information: 0 : [9520] Current OS installation type is 'Client'.
System.Net Information: 0 : [9520] RAS supported: True
System.Net Verbose: 0 : [9520] Exiting HttpWebRequest#58870012::HttpWebRequest() 
System.Net Verbose: 0 : [9520] Exiting WebRequest::Create()     -> HttpWebRequest#58870012
System.Net Verbose: 0 : [9520] HttpWebRequest#58870012::GetResponse()
System.Net Verbose: 0 : [9520] ServicePoint#33675143::ServicePoint(localhost:9876)
System.Net Information: 0 : [9520] Associating HttpWebRequest#58870012 with ServicePoint#33675143
System.Net Information: 0 : [9520] Associating Connection#34640832 with HttpWebRequest#58870012
System.Net.Sockets Verbose: 0 : [9520] Socket#43332040::Socket(AddressFamily#2)
System.Net.Sockets Verbose: 0 : [9520] Exiting Socket#43332040::Socket() 
System.Net.Sockets Verbose: 0 : [9520] Socket#54444047::Socket(AddressFamily#23)
System.Net.Sockets Verbose: 0 : [9520] Exiting Socket#54444047::Socket() 
System.Net.Sockets Verbose: 0 : [9520] DNS::TryInternalResolve(localhost)
System.Net.Sockets Verbose: 0 : [9520] Socket#43332040::Connect(127.0.0.1:9876#16787179)
System.Net.Sockets Error: 0 : [9520] Socket#43332040::UpdateStatusAfterSocketError() - ConnectionRefused
System.Net.Sockets Error: 0 : [9520] Exception in Socket#43332040::Connect - No connection could be made because the target machine actively refused it 127.0.0.1:9876.
System.Net.Sockets Verbose: 0 : [9520] Socket#43332040::Dispose()
System.Net.Sockets Verbose: 0 : [9520] Socket#54444047::Dispose()

关于c# - 消息为 "The request was aborted: The request was canceled."的 WebException 的根本原因是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33673939/

相关文章:

java - 带有问题网站的 HTTP 客户端 POST

c# - 调用和管理许多 http 请求的响应的最佳方式

c# - PropertyInfo.GetValue 返回 int,但转换为十进制会导致 InvalidCastException

c# - .net 中请求不等待响应

c# - 如何在 Winforms 项目中绘制笛卡尔点?

java - 要登录 Steam 需要 RSA key 。我已经使用 java 11 的 HttpClient 创建了一个发布请求,但它返回了 Body : {"success":false}

c# - 我应该使用 System.Net.Http Nuget 包吗?

c# - 如何在 Windows 窗体应用程序中显示 MFC 控件?

c# - 空传播 - 第二次空检查

.net - SQL Azure 与 WCF 性能对比