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.
How do you tell if a site is an HTTP:// site or an HTTPS:// site? If you type in something that looks like a URL with the scheme missing, then browsers will assume you intended to put http:// in front of it. Most browsers will hide the http:// part from the address bar for non-SSL sites.
Given a URL as a character string str of size N .The task is to check if the given URL is valid or not. The above URL is a valid URL. Note that there is a space after https://, hence the URL is invalid. An approach using java.net.url class to validate a URL is discussed in the previous post.
If you type in something that looks like a URL with the scheme missing, then browsers will assume you intended to put http:// in front of it. Most browsers will hide the http:// part from the address bar for non-SSL sites.
You could configure a server to redirect the URLs for some pages to HTTPS and some to HTTP. This was typically done to save on CPU power for pages where security wasn't needed. Today, CPU power is much cheaper, so it is normally better to use SSL by default.
try something like this:
public static bool IsHttps()
{
return HttpContext.Current.Request.IsSecureConnection;
}
Or if you working with asp.net-web-api you can check if Request.RequestUri.Scheme
is Uri.UriSchemeHttps
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With