我正在尝试从 HTTPS URL 读取 XML 文件:
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
using(WebClient client = new WebClient()) {
contents = client.DownloadString(dr["XmlImpotURL"].ToString() + dr["ApiKey"].ToString());
}
我收到此错误
System.Net.WebException:请求已中止:无法创建 SSL/TLS 安全通道。
我花了大约 2 个小时来解决这个问题,但我似乎找不到任何解决方案。
试试这个
string sVal = "http://www.w3schools.com/xml/note.xml";
XDocument document = XDocument.Load(sVal);
或
Uri url = new Uri("http://www.w3schools.com/xml/note.xml");
using (var wc = new WebClient())
{
string sss = wc.DownloadString(url);
}
如果您面临安全问题
试试这个
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
// allows for validation of SSL conversations
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
WebResponse respon = req.GetResponse();
Stream res = respon.GetResponseStream();
string ret = "";
byte[] buffer = new byte[1048];
int read = 0;
while ((read = res.Read(buffer, 0, buffer.Length)) > 0)
{
Console.Write(Encoding.ASCII.GetString(buffer, 0, read));
ret += Encoding.ASCII.GetString(buffer, 0, read);
}
return ret;
或
private void Test()
{ ServicePointManager.ServerCertificateValidationCallback += new
RemoteCertificateValidationCallback(Certificate);
}
private static bool Certificate(object sender, X509Certificate certificate,
X509Chain chain, SslPolicyErrors policyErrors) {
return true;
}
查看此链接以获取更多信息http://blogs.msdn.com/b/dgorti/archive/2005/09/18/471003.aspx
这已解决,而不是
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
我用过
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3;
现在可以工作了