【问题标题】:Avoiding the timeout exception while connecting to a https url to send request to a web-service连接到 https url 以向 Web 服务发送请求时避免超时异常
【发布时间】:2014-01-14 11:50:39
【问题描述】:

在 dotNet mvc 应用程序中,尝试向 http url 发送请求时,我的应用程序按预期工作。但是,当我尝试向 https url 发送请求时,出现以下错误(在 IIS 中发布应用程序并在 Web 浏览器中运行它时):

Server Error in '/dotnetstage' Application.
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 10.63.64.146:443
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Sockets.SocketException: 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 10.63.64.146:

我知道它与 SSL 证书有关,所以我发现有一种方法可以忽略证书验证:

 WebRequest webRequest = WebRequest.Create(reqURL);
            ServicePointManager.ServerCertificateValidationCallback += delegate { return true; }; //this line works for the certificate validation ignoring

即便如此,我也遇到了同样的错误。我有两个疑问:

  • 我是否在正确的位置添加了 ServicePointManager 行?如果不 您在代码中添加它的正确位置是什么?

  • 还有什么需要我注意的吗?

任何帮助或至少指向正确方法的指针将不胜感激。 Stack Overflow 在学习 c# mvc 应用程序编码和在 4 周内构建复杂的调用程序应用程序方面帮助了我很多,我也期待能快速解决这个问题。谢谢。 :)

【问题讨论】:

  • 如果您忽略证书的真实性,那么使用证书有什么意义?不应在生产中使用 ServerCertificateValidationCallback hack。
  • @scheien 好问题。目前,我们并不关心证书的真实性,否则我们会在代码中创建一个证书存储并对其进行处理。我们只是想演示代码的其他不依赖于 SSL 证书的功能。

标签: c# asp.net-mvc ssl


【解决方案1】:

在发出 Web 请求之前,应首先定义 ServerCertificateValidationCallback。但在您的情况下,问题完全不同,可能是 Web 服务器端代码在发送响应之前崩溃了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 2020-12-15
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多