1

how to Verify where website is using https, a secure communication protocol/not. like if i give www.facebook.com it should show https protected or not.im getting error in second line.im getting false even though site is 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;

        }
  • HttpContext.Current.Request.IsSecureConnection – Kaushik Maheta Nov 03 '15 at 12:00
  • 2
    He ask how to find if a remote site is https, not the `Current` one. – Aristos Nov 03 '15 at 12:09
  • i just want to detect a website is https protected or not. i will give any website name in text box it should tell protected or not. i dont know whether site having http or not. to make seo tool. general user give www.google.com then it should https protected or not – sanjay kumar Nov 03 '15 at 12:13

2 Answers2

0

there are many sites which works on both http and https, some works on http and some on https.

So in your url check twice

1) If http://+url is valid or not

2) if https://+url is valid or not.

Then you can get the result.

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# How can I check if a URL exists/is valid?

Community
  • 1
  • 1
amit dayama
  • 3,246
  • 2
  • 17
  • 28
-2

You can use the below line of code to check if the connection is secured or not.

HttpContext.Current.Request.IsSecureConnection
Kasun Koswattha
  • 2,302
  • 2
  • 12
  • 20
  • how to write a method to get that property. i didnt get it , can u help me out by creating sample method – sanjay kumar Nov 03 '15 at 12:05
  • In your question, the url is not in the correct format. if you want to create a Uri, you need to do the following. Uri baseUri = new Uri("https://www.stackoverflow.com"); string test = baseUri.Scheme; You have to mention the scheme (http:// or https://) – Kasun Koswattha Nov 03 '15 at 12:22