从https url读取XML文件

时间:2013-10-21 23:59:54

标签: c# https webrequest

我正在尝试从https网址

中读取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: The request was aborted: Could not create SSL/TLS secure channel.

我花了2个小时来解决这个问题,我似乎无法找到任何解决方案。

2 个答案:

答案 0 :(得分:0)

试试这个

        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

答案 1 :(得分:0)

这已解决,而不是

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;

我用过

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3;

现在它正在运转,谢谢大家的答案。