0

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 vb.net ?

If the user gives just stackoverflow.com without any prefix, I must be able to identify that it is an HTTP one

john77222
  • 21
  • 4
  • 3
    Well, `stackoverflow.com` is *a HTTPS* :) You don't know what protocol will be determined and if you'll be redirected to a HTTPS resource, even if the HTTP protocol is specified in an URI. You have to go for it and see the URI of the Response. Is there a reason why you'd like to know this in advance? – Jimi Jul 20 '20 at 12:33
  • see this link https://learn.microsoft.com/en-us/dotnet/api/system.web.httprequest.issecureconnection?view=netframework-4.8 – john77222 Jul 20 '20 at 12:37
  • What should I see there? As mentioned, you have to establish a connection and see what happens. See also: [Which TLS version was negotiated?](https://stackoverflow.com/a/48675492/7444103) – Jimi Jul 20 '20 at 12:41
  • you can Create code to System.Web library – john77222 Jul 20 '20 at 12:52
  • Sorry, I don't know what that means. – Jimi Jul 20 '20 at 12:58
  • you can use this library System.Web – john77222 Jul 20 '20 at 13:02
  • To do what, exactly? If you need to better qualify your question, click the `edit` link and add more information about your requirements or what you want to achieve. -- As mentioned, you cannot determine off-line the protocol which will be used in a Connection to a remote resource, if that's what you're asking. – Jimi Jul 20 '20 at 13:08

1 Answers1

-1

Try this:

In case that the url is plain without http/https check it with the below code:

    'Validates a URL.
    Function ValidateUrl(url As String) As Boolean
        Dim validatedUri As Uri = Nothing
        If (Uri.TryCreate(url, UriKind.Absolute, validatedUri)) Then
            Return (validatedUri.Scheme = Uri.UriSchemeHttp Or validatedUri.Scheme = Uri.UriSchemeHttps)
        End If
        Return False
    End Function

And then do something if http/https exists:

Dim u as new System.Uri("http://www.hello.com")
If u.Scheme = "http" Then
   ..do something..
Else
   ..do something else..
Isidoros Moulas
  • 520
  • 3
  • 10