System.Net.Sockets.SocketException

发布于 2024-08-03 19:45:59 字数 444 浏览 9 评论 0原文

我有一个通过 http(s) 对外部资源发出同步请求的应用程序。每隔几周,我就会收到以下异常

Exception: System.Net.WebException - Message: Unable to connect to the remote server

内部异常:System.Net.Sockets.SocketException - 消息:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应

令我困扰的是,我的跟踪日志显示,对于此异常的每个实例,它都被抛出“21 秒”到请求中。

有人以前见过这样的行为吗?有解决方案吗? System.Net.HttpWebRequest 命名空间中是否存在任何已知问题? 使用 System.Net.HttpWebRequest 的最佳实践是什么,例如我应该显式关闭并处置响应流吗?

I have an application that makes a sync request on an external resource via http(s). Every few weeks I get the following exception

Exception: System.Net.WebException - Message: Unable to connect to the remote server

Inner Exception: System.Net.Sockets.SocketException - Message: 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

What is bothering me is that my trace logs show that for every single instance of this exception, it was thrown "21 seconds" into the request.

Has anybody seen behaviour like this before and is there a solution?
Are there any known issues within the System.Net.HttpWebRequest namespace?
What are the best practices for using System.Net.HttpWebRequest e.g. should I explicitly close and dispose of response streams...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪你搞怪i 2024-08-10 19:45:59

如果您正在访问外部资源,答案很可能是这些资源不可用(他们的服务器已关闭,您的连接已关闭,他们的连接已关闭等)。

您可以在 HTTPWebRequest 对象的 GetResponse() 方法之前设置 .Timeout 属性,以延长正式超时之前的时间,看看会发生什么:

myHttpWebRequest.Timeout = 10; //In milliseconds

如果失败,您可以尝试将其添加到您的配置文件中,如果您运行 ASP.NET NETWORK SERVICE 作为您的主机帐户:

<configuration >
  <system.net>
    <defaultProxy>
      <proxy bypassonlocal="true" usesystemdefault="false" />
    </defaultProxy>
</system.net>
</configuration>

如果不这样做,您的应用程序将无法访问代理设置。 .NET搜索它,找不到它,然后默认连接。这将导致异常,但执行将继续。这或许可以解释为什么请求总是需要 21 秒。

在此处阅读完整详细信息

If you're accessing external resources, the answer is most likely that those resources weren't available (their servers were down, your connection was down, their connection was down, etc).

You can set the .Timeout property before the GetResponse() method of the HTTPWebRequest object to extend the time before it officially times out and see what happens:

myHttpWebRequest.Timeout = 10; //In milliseconds

Failing this, you could try adding this to your configuration file if your running ASP.NET with NETWORK SERVICE as your host account:

<configuration >
  <system.net>
    <defaultProxy>
      <proxy bypassonlocal="true" usesystemdefault="false" />
    </defaultProxy>
</system.net>
</configuration>

If you don't, your application can't get to the proxy settings. .NET search for it, can't find it and then connects via default. This will cause the exception but the excution will continue. It may explain why it's always 21 seconds into a request.

Read the full details here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文