基础连接已关闭:-WebClient错误

e-o*_*-on 0 c# asp.net webclient

当我尝试单击一个单独的asp页面WebClient用于从邮政编码下载完整的地址详细信息时,我一直收到错误消息。

The underlying connection was closed: An unexpected error occurred on a send.
Run Code Online (Sandbox Code Playgroud)

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

这是我正在使用的代码(摘自此处的另一篇文章)

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));
}
Run Code Online (Sandbox Code Playgroud)

最初我只是使用它,结果相同:

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

string data = client.DownloadString(address);
Run Code Online (Sandbox Code Playgroud)

该应用程序使用.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();
Run Code Online (Sandbox Code Playgroud)

该错误消息包含:

System.Threading.Thread.AbortInternal() at System.Threading.Thread.Abort(Object stateInfo) at System.Web.HttpResponse.End() at MyApp.MyPage.WebClientTest()
Run Code Online (Sandbox Code Playgroud)

不知道这是否会帮助任何人...

e-o*_*-on 6

摘自 Jamby 在相关评论中的回答:

您是否尝试过使用 http 而不是 https 来访问它?

简单的。谢谢


tbo*_*one 6

只是以为我会加这个,对我有用。某些站点或防火墙按其版本阻止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);
                }
Run Code Online (Sandbox Code Playgroud)

使用的默认TLS版本可能取决于使用的.NET框架,并且.NET 4.0似乎缺少枚举(因此3072)。较新的版本应该可以执行以下操作

SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参见此处