URI methods don't really work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anil Gupte

    URI methods don't really work?

    Both
    Uri.CheckHostNa me(Name)
    and
    Uri.CheckScheme Name(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,
    --
    Anil Gupte
    Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving





  • Michel Posseth [MCP]

    #2
    Re: URI methods don't really work?

    You may use this method

    Public Function UriIsValid(ByVa l uri As String) As Boolean

    Dim ret As Boolean = False

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

    Try

    Dim WebReq As HttpWebRequest = HttpWebRequest. Create(uri)

    Using WebResp As HttpWebResponse = WebReq.GetRespo nse()

    ret = True

    End Using

    Catch ex As Exception

    Return False

    End Try

    Return ret

    End Function


    HTH

    Michel




    "Anil Gupte" <anil-list@icinema.co mschreef in bericht
    news:eFcatBvoIH A.4904@TK2MSFTN GP03.phx.gbl...
    Both
    Uri.CheckHostNa me(Name)
    and
    Uri.CheckScheme Name(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,
    --
    Anil Gupte
    Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving


    >
    >
    >

    Comment

    • Smokey Grindel

      #3
      Re: URI methods don't really work?

      might want to mention that would require an active internet or DNS server
      connection to work ;)

      "Michel Posseth [MCP]" <msnews@posseth .comwrote in message
      news:%23Hz8vWvo IHA.2208@TK2MSF TNGP04.phx.gbl. ..
      You may use this method
      >
      Public Function UriIsValid(ByVa l uri As String) As Boolean
      >
      Dim ret As Boolean = False
      >
      If uri.ToLower().S tartsWith("www. ") Then uri = String.Concat(" http://",
      uri)
      >
      Try
      >
      Dim WebReq As HttpWebRequest = HttpWebRequest. Create(uri)
      >
      Using WebResp As HttpWebResponse = WebReq.GetRespo nse()
      >
      ret = True
      >
      End Using
      >
      Catch ex As Exception
      >
      Return False
      >
      End Try
      >
      Return ret
      >
      End Function
      >
      >
      HTH
      >
      Michel
      >
      >
      >
      >
      "Anil Gupte" <anil-list@icinema.co mschreef in bericht
      news:eFcatBvoIH A.4904@TK2MSFTN GP03.phx.gbl...
      >Both
      >Uri.CheckHostN ame(Name)
      >and
      >Uri.CheckSchem eName(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,
      >--
      >Anil Gupte
      >www.keeninc.net
      >www.icinema.com
      >>
      >>
      >>
      >
      >

      Comment

      • Michel Posseth [MCP]

        #4
        Re: URI methods don't really 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(ByVa l URI As String) As Boolean

        If uri.ToLower().S tartsWith("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_-]*).?([a-zA-Z0-9_-]*).?([a-zA-Z0-9_-]*)))/?([a-zA-Z0-9_/?%=&+#.-~]*)$"

        Dim regex As New System.Text.Reg ularExpressions .Regex(RegexPat tern)

        Return regex.IsMatch(U RI)

        End Function




        "Smokey Grindel" <nospam@nospam. comschreef in bericht
        news:uevz2jvoIH A.3408@TK2MSFTN GP03.phx.gbl...
        might want to mention that would require an active internet or DNS server
        connection to work ;)
        >
        "Michel Posseth [MCP]" <msnews@posseth .comwrote in message
        news:%23Hz8vWvo IHA.2208@TK2MSF TNGP04.phx.gbl. ..
        >You may use this method
        >>
        >Public Function UriIsValid(ByVa l uri As String) As Boolean
        >>
        >Dim ret As Boolean = False
        >>
        >If uri.ToLower().S tartsWith("www. ") Then uri = String.Concat(" http://",
        >uri)
        >>
        >Try
        >>
        >Dim WebReq As HttpWebRequest = HttpWebRequest. Create(uri)
        >>
        >Using WebResp As HttpWebResponse = WebReq.GetRespo nse()
        >>
        >ret = True
        >>
        >End Using
        >>
        >Catch ex As Exception
        >>
        >Return False
        >>
        >End Try
        >>
        >Return ret
        >>
        >End Function
        >>
        >>
        >HTH
        >>
        >Michel
        >>
        >>
        >>
        >>
        >"Anil Gupte" <anil-list@icinema.co mschreef in bericht
        >news:eFcatBvoI HA.4904@TK2MSFT NGP03.phx.gbl.. .
        >>Both
        >>Uri.CheckHost Name(Name)
        >>and
        >>Uri.CheckSche meName(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,
        >>--
        >>Anil Gupte
        >>www.keeninc.net
        >>www.icinema.com
        >>>
        >>>
        >>>
        >>
        >>
        >
        >

        Comment

        • Anil Gupte

          #5
          Re: URI methods don't really work?

          Ah! That one is muh better.

          Still I wish stuff MS gave you actually worked!

          --
          Anil Gupte
          Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving



          "Michel Posseth [MCP]" <msnews@posseth .comwrote in message
          news:OI9TYzvoIH A.5836@TK2MSFTN GP04.phx.gbl...
          :-) 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(ByVa l URI As String) As Boolean
          >
          If uri.ToLower().S tartsWith("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_-]*).?([a-zA-Z0-9_-]*).?([a-zA-Z0-9_-]*)))/?([a-zA-Z0-9_/?%=&+#.-~]*)$"
          >
          Dim regex As New System.Text.Reg ularExpressions .Regex(RegexPat tern)
          >
          Return regex.IsMatch(U RI)
          >
          End Function
          >
          >
          >
          >
          "Smokey Grindel" <nospam@nospam. comschreef in bericht
          news:uevz2jvoIH A.3408@TK2MSFTN GP03.phx.gbl...
          >might want to mention that would require an active internet or DNS server
          >connection to work ;)
          >>
          >"Michel Posseth [MCP]" <msnews@posseth .comwrote in message
          >news:%23Hz8vWv oIHA.2208@TK2MS FTNGP04.phx.gbl ...
          >>You may use this method
          >>>
          >>Public Function UriIsValid(ByVa l uri As String) As Boolean
          >>>
          >>Dim ret As Boolean = False
          >>>
          >>If uri.ToLower().S tartsWith("www. ") Then uri = String.Concat(" http://",
          >>uri)
          >>>
          >>Try
          >>>
          >>Dim WebReq As HttpWebRequest = HttpWebRequest. Create(uri)
          >>>
          >>Using WebResp As HttpWebResponse = WebReq.GetRespo nse()
          >>>
          >>ret = True
          >>>
          >>End Using
          >>>
          >>Catch ex As Exception
          >>>
          >>Return False
          >>>
          >>End Try
          >>>
          >>Return ret
          >>>
          >>End Function
          >>>
          >>>
          >>HTH
          >>>
          >>Michel
          >>>
          >>>
          >>>
          >>>
          >>"Anil Gupte" <anil-list@icinema.co mschreef in bericht
          >>news:eFcatBvo IHA.4904@TK2MSF TNGP03.phx.gbl. ..
          >>>Both
          >>>Uri.CheckHos tName(Name)
          >>>and
          >>>Uri.CheckSch emeName(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,
          >>>--
          >>>Anil Gupte
          >>>www.keeninc.net
          >>>www.icinema.com
          >>>>
          >>>>
          >>>>
          >>>
          >>>
          >>
          >>
          >
          >

          Comment

          • Steve Gerrard

            #6
            Re: URI methods don't really work?

            Anil Gupte wrote:
            Ah! That one is muh better.
            >
            Still I wish stuff MS gave you actually worked!
            >
            >
            URI's can be relative, so just testing a string directly won't tell you much.

            A better test to use is the IsWellFormedUri String function, specifying an
            absolute uri.

            This is a little stricter than the Regex in Michel's post. If you add Michel's
            test for "www", you can get a pretty good test:

            If myURI.ToLower() .StartsWith("ww w.") Then
            myURI = String.Concat(" http://", myURI)
            End If
            If Uri.IsWellForme dUriString(myUR I, UriKind.Absolut e) then
            ' now you are getting somewhere
            End If



            Comment

            • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

              #7
              RE: URI methods don't really work?

              The remarks sections in MSDN describe pretty well why you are seeing the
              behaviour. It also explains that CheckHostName(n ame) doesn't return a
              boolean, but rather a UriHostNameType value.

              "Anil Gupte" wrote:
              Both
              Uri.CheckHostNa me(Name)
              and
              Uri.CheckScheme Name(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,
              --
              Anil Gupte
              Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving


              >
              >
              >
              >

              Comment

              • Anil Gupte

                #8
                Re: URI methods don't really work?

                Yes, in fact I am using this fucntion:

                Public Function IsValidURI(ByVa l Name As String) As Boolean
                Dim ValidURI As Boolean = False
                If Uri.IsWellForme dUriString(Name , UriKind.Relativ eOrAbsolute) Then
                ValidURI = True
                Else
                ValidURI = False
                End If
                Name = Name.Replace("h ttp://", "")
                If Uri.CheckHostNa me(Name) Then
                ValidURI = True
                Else
                ValidURI = False
                End If
                If Uri.CheckScheme Name(Name) Then
                ValidURI = True
                Else
                ValidURI = False
                End If
                Return ValidURI
                End Function

                And only the first one works - that is URI.IsWellForme dUriString

                --
                Anil Gupte
                Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving



                "Steve Gerrard" <mynamehere@com cast.netwrote in message
                news:m46dnVNtUK DcGJbVnZ2dnUVZ_ hCdnZ2d@comcast .com...
                Anil Gupte wrote:
                >Ah! That one is muh better.
                >>
                >Still I wish stuff MS gave you actually worked!
                >>
                >>
                >
                URI's can be relative, so just testing a string directly won't tell you
                much.
                >
                A better test to use is the IsWellFormedUri String function, specifying an
                absolute uri.
                >
                This is a little stricter than the Regex in Michel's post. If you add
                Michel's test for "www", you can get a pretty good test:
                >
                If myURI.ToLower() .StartsWith("ww w.") Then
                myURI = String.Concat(" http://", myURI)
                End If
                If Uri.IsWellForme dUriString(myUR I, UriKind.Absolut e) then
                ' now you are getting somewhere
                End If
                >
                >
                >

                Comment

                • Anil Gupte

                  #9
                  Re: URI methods don't really work?

                  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,
                  --
                  Anil Gupte
                  Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving



                  "Family Tree Mike" <FamilyTreeMike @discussions.mi crosoft.comwrot e in
                  message news:FD3FE25A-86D6-4675-9AB9-E7A5011E45BE@mi crosoft.com...
                  The remarks sections in MSDN describe pretty well why you are seeing the
                  behaviour. It also explains that CheckHostName(n ame) doesn't return a
                  boolean, but rather a UriHostNameType value.
                  >
                  "Anil Gupte" wrote:
                  >
                  >Both
                  >Uri.CheckHostN ame(Name)
                  >and
                  >Uri.CheckSchem eName(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,
                  >--
                  >Anil Gupte
                  >www.keeninc.net
                  >www.icinema.com
                  >>
                  >>
                  >>
                  >>

                  Comment

                  Working...