20

I need to check if a given URL (which is not necessarily prefixed with http or https) is HTTP or HTTPs.
Is this possible in C#?
If the user gives just www.dotnetperls.com without any prefix, I must be able to identify that it is an HTTP one. Tried the following,

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("www.dotnetpearls.com");         
 string u = request.RequestUri.Scheme;

But this gives an Invalid URL error. It expects the protocol to be specified.

7
  • 4
    your question does not make sense. if an url does not have https it is http Commented Dec 19, 2013 at 10:27
  • 1
    But in my case, the user may enter any url say, www.wikipedia.org, which is actually an HTTPS. So in this case I need to find if its is an HTTPS or HTTP and display a warning message accordingly only if HTTP. Commented Dec 19, 2013 at 10:46
  • 3
    you have to use webclient and make a request to http and check if it is http or https with Request.IsSecureConnection Commented Dec 19, 2013 at 10:58
  • Can you provide an example URL that doesn't contain the protocol? Commented Dec 19, 2013 at 13:24
  • 1
    web requests are BY DEFAULT http. The client can NOT simply ignore that and start firing off https requests. Not all sites are https-enabled. The initial request, if you don't explicitly specify https in the url, HAS to be http, and then the remote server can issue a redirect and point you at the https version instead. But regardless, your initial protocol-less-url hit HAS to be http. HTTP 2.0 will apparently change to an ssl-first-by-default mode, but that's at some vague point in the future, not "today". Commented Dec 19, 2013 at 14:13

6 Answers 6

13

try something like this:

public static bool IsHttps()
{
    return HttpContext.Current.Request.IsSecureConnection;
}

Or if you working with you can check if Request.RequestUri.Scheme is Uri.UriSchemeHttps.

Sign up to request clarification or add additional context in comments.

1 Comment

but I all I have is just a url without http/https prefix and not a http request itself.
11
var uri = new Uri("https://sss.com");
var requestType= uri.Scheme;

Here the requestType will will give you whether the request is of type http or https

2 Comments

My url does not contain a http/https prefix. So cant use this, since it will show "URI not in correct format error".
@wickjon You can check if it's absolute first, then check if it's https bool isSecure = uri.IsAbsoluteUri && uri.Scheme.Equals("https")
2
if (url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
}

Comments

1

I've been looking for a solution for the same as well and unfortunately I couldn't find any.

And also some websites doesn't support www as well.

So I'm sending 4 requests to find out if the website supports https and finding the one that supports.

I know this is not a good solution but it works at least. If the first one works, I'm not calling the rest of them...

  1. WebRequest.Create("https://dotnetpearls.com");
  2. WebRequest.Create("https://www.dotnetpearls.com");
  3. WebRequest.Create("http://dotnetpearls.com");
  4. WebRequest.Create("http://www.dotnetpearls.com");

Comments

0

You can use HttpRequest.IsSecureConnection

HttpContext.Current.Request.IsSecureConnection   

OR, where request object is available.

Request.IsSecureConnection

1 Comment

but I all I have is just a url without http/https prefix and not a http request itself.
0

For ASP.net core the property Request.Scheme is available that works for me:

var uri = String.Format("{0}://{1}",Request.Scheme,Request.Host.Value);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.