Created
October 20, 2025 05:59
-
-
Save donma/64e6298a7ac77b22badcdafc24afda34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void CheckSslCertificate(string url) | |
{ | |
try | |
{ | |
// 建立 HttpClientHandler 並攔截憑證驗證 | |
using var handler = new HttpClientHandler(); | |
handler.ServerCertificateCustomValidationCallback = (request, cert, chain, errors) => | |
{ | |
var x509 = new X509Certificate2(cert); | |
Console.WriteLine("=== SSL 憑證資訊 ==="); | |
Console.WriteLine("網站:" + url); | |
Console.WriteLine("主題:" + x509.Subject); | |
Console.WriteLine("頒發者:" + x509.Issuer); | |
Console.WriteLine("生效日:" + x509.NotBefore); | |
Console.WriteLine("到期日:" + x509.NotAfter); | |
Console.WriteLine("是否過期:" + (DateTime.Now > x509.NotAfter ? "是" : "否")); | |
Console.WriteLine("剩餘天數:" + (x509.NotAfter - DateTime.Now).TotalDays + " 天"); | |
Console.WriteLine("===================="); | |
return true; | |
}; | |
using var client = new HttpClient(handler); | |
// 模擬瀏覽器 User-Agent,避免被 CDN 阻擋 | |
client.DefaultRequestHeaders.Add( | |
"User-Agent", | |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + | |
"(KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36" | |
); | |
var task = client.GetAsync(url); | |
task.Wait(); | |
var response = task.Result; | |
Console.WriteLine("HTTP 狀態碼: " + (int)response.StatusCode); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("發生錯誤:" + ex.Message); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CheckSslCertificate("https://www.google.com"); | |
CheckSslCertificate("https://blog.no2don.com"); | |
//沒有SSL | |
CheckSslCertificate("https://no2don.com"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment