2013-10-21 127 views
0

我試圖從HTTPS URL讀取XML文件的URL

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小時解決這一點,我讀一個xml文件似乎無法找到任何解決方案。

+1

您是否使用'ex.ToString()'顯示了整個異常?可能有一個'InnerException'試圖更詳細地告訴你問題是什麼。 –

+0

對於示例代碼,提供代表性示例數據而不是隨機屬性好得多(例如'DownloadString(「https:// myserver/foo?bar = 222」)'而不是'DownloadString(NotGoingToShowValue)') –

+0

檢查我的回答如果你仍然面臨問題...分享的網址,並告訴我們你需要激動...什麼是你提到的「博士」 – Aravind

回答

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; 
         } 

檢查此鏈接而不是

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

更多信息http://blogs.msdn.com/b/dgorti/archive/2005/09/18/471003.aspx

0

這是解決了,我用

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

現在,它的工作,謝謝大家對你的答案。