【发布时间】:2015-09-25 06:48:30
【问题描述】:
我正在使用 .Net(框架 3.5)以类似的方式访问许多网站以获取数据(用于 Wikipedia)。现在我遇到了一个我无法访问的站点。我收到一个异常“底层连接已关闭”。该站点使用 SSL (https),但我不知道这是否会引发问题。我已经阅读了许多主题并尝试了许多建议,但对我来说还没有任何效果。我也尝试过 OpenRead / ReadToEnd,它也失败了。我必须承认,我对 HTTP 的了解非常有限。
非常欢迎任何建议!
杰尔
这是我的代码示例:
class WebClientDBLink : WebClient
{
public WebClientDBLink()
{
UseDefaultCredentials = true;
Encoding = Encoding.UTF8;
Headers.Add("Content-Type", Bot.webContentType);
Headers.Add("User-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
}
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest wr = (HttpWebRequest)base.GetWebRequest(address);
wr.CookieContainer = new CookieContainer();
// wr.KeepAlive = false;
// wr.Timeout = 100000;
return wr;
}
}
...
String sUrl = "https://www.infoflora.ch/de/flora/2906-.html"; // just one exampl
String sText = "";
try
{
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
// ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
Uri res = new Uri(sUrl);
WebClient wc = new WebClientDBLink();
sText = wc.DownloadString(res);
}
catch (Exception e)
{
...
return false;
}
【问题讨论】:
-
你是在 Windows XP 上运行这个吗?
-
是的,它在 Windows XP 上运行。对不起,我忘记了。