URI methods don't really work?

  • Thread starter Thread starter Anil Gupte
  • Start date Start date
A

Anil Gupte

Both
Uri.CheckHostName(Name)
and
Uri.CheckSchemeName(Name)

return true no matter what I use in "Name" (for example asdasd.sdf)

Are these actually supposed to work? Is there a better way to check if a
user has entered a valid URL?

Thanx,
 
You may use this method

Public Function UriIsValid(ByVal uri As String) As Boolean

Dim ret As Boolean = False

If uri.ToLower().StartsWith("www.") Then uri = String.Concat("http://", uri)

Try

Dim WebReq As HttpWebRequest = HttpWebRequest.Create(uri)

Using WebResp As HttpWebResponse = WebReq.GetResponse()

ret = True

End Using

Catch ex As Exception

Return False

End Try

Return ret

End Function


HTH

Michel
 
might want to mention that would require an active internet or DNS server
connection to work ;)
 
:-) Thank you Smokey as i am on broadband line connected it has become so
normall to me that everyone is always connected

but for the case he is not

Public Function IsvalidURI(ByVal URI As String) As Boolean

If uri.ToLower().StartsWith("www.") Then uri = String.Concat("http://",
URI)


Dim RegexPattern As String =
"^((http://)|(https://))((([a-zA-Z0-9_-...[a-zA-Z0-9_-]*)))/?([a-zA-Z0-9_/?%=&+#.-~]*)$"

Dim regex As New System.Text.RegularExpressions.Regex(RegexPattern)

Return regex.IsMatch(URI)

End Function
 
Ah! That one is muh better.

Still I wish stuff MS gave you actually worked!

--
Anil Gupte
www.keeninc.net
www.icinema.com

Michel Posseth said:
:-) Thank you Smokey as i am on broadband line connected it has become
so normall to me that everyone is always connected

but for the case he is not

Public Function IsvalidURI(ByVal URI As String) As Boolean

If uri.ToLower().StartsWith("www.") Then uri = String.Concat("http://",
URI)


Dim RegexPattern As String =
"^((http://)|(https://))((([a-zA-Z0-9_-...[a-zA-Z0-9_-]*)))/?([a-zA-Z0-9_/?%=&+#.-~]*)$"

Dim regex As New System.Text.RegularExpressions.Regex(RegexPattern)

Return regex.IsMatch(URI)

End Function




Smokey Grindel said:
might want to mention that would require an active internet or DNS server
connection to work ;)
 
The remarks sections in MSDN describe pretty well why you are seeing the
behaviour. It also explains that CheckHostName(name) doesn't return a
boolean, but rather a UriHostNameType value.
 
Yes, in fact I am using this fucntion:

Public Function IsValidURI(ByVal Name As String) As Boolean
Dim ValidURI As Boolean = False
If Uri.IsWellFormedUriString(Name, UriKind.RelativeOrAbsolute) Then
ValidURI = True
Else
ValidURI = False
End If
Name = Name.Replace("http://", "")
If Uri.CheckHostName(Name) Then
ValidURI = True
Else
ValidURI = False
End If
If Uri.CheckSchemeName(Name) Then
ValidURI = True
Else
ValidURI = False
End If
Return ValidURI
End Function

And only the first one works - that is URI.IsWellFormedUriString
 
Hmm, thanx for that. I seem to remember it was a boolean return type but I
am obviously mistaken - let me try it.

Thanx again,
 
Back
Top