2017-04-01 46 views
-2

我用這個代碼來獲取網頁內容的Web響應,但在WebResponse myResponse = myRequest.GetResponse();不能得到以https

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://www.ecfr.gov"); 
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (Object obj, X509Certificate X509certificate, X509Chain chain, System.Net.Security.SslPolicyErrors errors) 
{ 
    return true; 
}; 
myRequest.Method = "GET"; 
WebResponse myResponse = myRequest.GetResponse(); 
StreamReader sr = new System.IO.StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8); 
string result = sr.ReadToEnd(); 
sr.Close(); 
myResponse.Close(); 

得到例外,我知道它需要SSL/TLS證書,但我想知道爲什麼我仍然得到一個錯誤?

https://www.ecfr.gov

+0

任何時候你「獲得例外」您的需要告訴我們的異常是什麼。 –

回答

0

試試這個

static void Main(string[] args) 
    { 
     ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; 
     var client = new HttpClient(); 
     var result = client.GetAsync("https://www.ecfr.gov").Result; 
     var str = result.Content.ReadAsStringAsync().Result; 
    }