如何知道给定的网站是否在C#中受https保护

本文关键字:中受 https 保护 是否 网站 何知道 | 更新日期: 2023-09-27 18:20:25

如何验证网站是否使用https(一种安全的通信协议)。比如,如果我给www.facebook.com,它应该显示https受保护或不受保护。我在第二行中出错。即使网站是https ,我也会出错

  checkSecured();
  private void checkSecured()
{
    string url = txturl.Text.Trim();
    var uri = new Uri("https://www.facebook.com");
    var requestType = uri.Scheme;
    var value= HttpContext.Current.Request.IsSecureConnection;
        }

如何知道给定的网站是否在C#中受https保护

有许多网站同时使用http和https,有些网站使用http,有些网站则使用https。

所以在你的url中检查两次

1) 如果http://+url有效或不是

2) https://+url是否有效。

然后你就可以得到结果了。

private bool RemoteFileExists(string url)
{
try
{
    //Creating the HttpWebRequest
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    //Setting the Request method HEAD, you can also use GET too.
    request.Method = "HEAD";
    //Getting the Web Response.
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    //Returns TRUE if the Status code == 200
    response.Close();
    return (response.StatusCode == HttpStatusCode.OK);
}
catch
{
    //Any exception will returns false.
    return false;
}
}

from:C#如何检查URL是否存在/有效?

您可以使用下面的代码行来检查连接是否安全。

HttpContext.Current.Request.IsSecureConnection