【问题标题】:The underlying connection was closed: - WebClient error底层连接已关闭:- WebClient 错误
【发布时间】:2014-05-31 03:48:44
【问题描述】:

当我尝试使用 WebClient 访问单独的 asp 页面以从邮政编码下载完整的地址详细信息时,我不断收到错误消息。

The underlying connection was closed: An unexpected error occurred on a send.

这只发生在服务器上。不仅如此,当我将 URL 粘贴到浏览器中时,它可以完美运行。这可能是防火墙设置吗?

这是我正在使用的代码(取自 here 上的另一篇帖子)

using (CookieAwareWebClient WC = new CookieAwareWebClient())
{
    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3;

    string data = WC.DownloadString("https://www.myUrl.com/postcode.asp?postcode=" + postcode + "&houseNumber=" + Server.HtmlEncode(txtHouseNumber.Text));
}

原来我只是用这个,结果一样:

WebClient client = new WebClient();
string address = "https://www.myUrl.com/postcode.asp?postcode=" + postcode + "&houseNumber=" + Server.HtmlEncode(txtHouseNumber.Text);

string data = client.DownloadString(address);

该应用程序是使用 .NET 4/C# 构建的,并托管在带有 iis6 的 Windows Server 2003 上。

如果这是防火墙或安全设置,会是什么?如果没有,关于原因或解决方法的任何想法?非常感谢

更新 - ########################

好的,我也尝试过使用 HttpWebRequest,但第二行出现错误:

HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(new Uri("https://www.myUrl.com/postcode.asp?postcode=AZ11ZA&houseNumber=1"));
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();

错误信息包含:

System.Threading.Thread.AbortInternal() at System.Threading.Thread.Abort(Object stateInfo) at System.Web.HttpResponse.End() at MyApp.MyPage.WebClientTest()

不知道对大家有没有帮助...

【问题讨论】:

  • 您可以通过网络浏览器访问网址吗?
  • 应用程序是用 .NET 构建的,但您请求的是 ASP 页面?
  • 是的,URL 在浏览器中没有问题
  • @Cervesar - 是的,我知道这听起来很奇怪,但所有 asp 页面所做的都是使用邮政编码和门牌号访问外部服务并返回所有可能性的字符串。我也尝试使用包含我期望的信息的 .txt 文件,但结果相同。
  • 您是否尝试过在 http 而不是 https 中访问它?

标签: c# asp.net webclient


【解决方案1】:

取自 Jamby 在问题 cmets 中的回答:

您是否尝试过在 http 而不是 https 中访问它?

简单。谢谢

【讨论】:

  • 谢谢,这在尝试访问 Zendesk api 时对我有用。
【解决方案2】:

只是想我会添加这个,这对我有用。某些站点或防火墙会按其版本阻止 SSL 或 TLS 连接。最近,这意味着任何低于 1.2 的版本。所以,我在从网上下载文件之前设置了以下内容:

using (WebClient webClient = new WebClient())
                {
                    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
                    ServicePointManager.DefaultConnectionLimit = 9999;

                    log.Info("Downloading file");
                    webClient.DownloadFile("https://somesitedomain/somefile.csv", outfile);
                }

使用的默认 TLS 版本可能取决于使用的 .NET 框架,并且 .NET 4.0 上似乎缺少枚举(因此是 3072)。更新的版本应该能够做类似的事情

SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12

欲了解更多信息,请参阅here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 2013-10-31
    相关资源
    最近更新 更多