Any way to check a url

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Henry Jia

    Any way to check a url

    Hi,

    Does there has any easy way to check if a specified url is a valid one (both the format should be valid but also the specified path / file is available)?

    Thanks in advance!

    Henry
  • Scott Allen

    #2
    Re: Any way to check a url

    You could always pop the url into a System.Net.WebC lient object. If
    the url is invalid, or the url is unreachable, the object will throw a
    WebException. Of course, this doesn't distinguish between a bad URL
    and a bad network connection, for that you would need some additional
    tests, perhaps fetching from a known good URL.

    HTH,

    --
    Scott


    On Tue, 27 Apr 2004 11:46:53 +0800, "Henry Jia" <jia_h@hotmail. com>
    wrote:
    [color=blue]
    >Hi,
    >
    > Does there has any easy way to check if a specified url is a valid one (both the format should be valid but also the specified path / file is available)?
    >
    > Thanks in advance!
    >
    >Henry[/color]

    Comment

    • Henry Jia

      #3
      Re: Any way to check a url

      What's the meaning of "pop the url"? and how about the time used for check?

      Thank you!

      "Scott Allen" <bitmask@[nospam].fred.net> wrote in message news:00or80hvsa 9sqsshtt8qdqvi6 8ipq2ei09@4ax.c om...
      You could always pop the url into a System.Net.WebC lient object. If
      the url is invalid, or the url is unreachable, the object will throw a
      WebException. Of course, this doesn't distinguish between a bad URL
      and a bad network connection, for that you would need some additional
      tests, perhaps fetching from a known good URL.

      HTH,

      --
      Scott


      On Tue, 27 Apr 2004 11:46:53 +0800, "Henry Jia" <jia_h@hotmail. com>
      wrote:
      [color=blue]
      >Hi,
      >
      > Does there has any easy way to check if a specified url is a valid one (both the format should be valid but also the specified path / file is available)?
      >
      > Thanks in advance!
      >
      >Henry[/color]

      Comment

      • Morten Wennevik

        #4
        Re: Any way to check a url

        Hi Henry,

        So far this method has worked for me, it connects to a DNS server and
        checks the name. I strip any http:// headers or folder structures before
        passing it to ValidateURL.

        // check to see if we can find the url
        private bool ValidateURL(str ing u)
        {
        try
        {
        Dns.GetHostByNa me(u);
        }
        catch
        {
        try // maybe we passed an ip address
        {
        Dns.GetHostByAd dress(u);
        }
        catch
        {
        return false;
        }
        }
        return true;
        }


        Happy coding!
        Morten Wennevik [C# MVP]

        Comment

        • Scott Allen

          #5
          Re: Any way to check a url

          Hi Henry,

          Sorry for the slang.

          Here is a sample:

          WebClient wc = new WebClient();
          Stream stream = wc.OpenRead("ht tp://www.microsoft.c om");
          stream.ReadByte ();
          stream.Close();

          If you pass the URL into the OpenRead method, and the code executes
          without an exception, then you can be sure that the URL is well formed
          and valid, that the server is reachable, and that the path and
          filename exist on the server.

          As for timing, it won't be as fast as verifying the URL format with a
          regular expression, but the only way to be sure you have a valid path
          to a file that exists on a remote server is to go to the server and
          ask for the file.

          HTH,

          On Tue, 27 Apr 2004 13:24:08 +0800, "Henry Jia" <jia_h@hotmail. com>
          wrote:
          [color=blue]
          >What's the meaning of "pop the url"? and how about the time used for check?
          >
          >Thank you!
          >
          > "Scott Allen" <bitmask@[nospam].fred.net> wrote in message news:00or80hvsa 9sqsshtt8qdqvi6 8ipq2ei09@4ax.c om...
          > You could always pop the url into a System.Net.WebC lient object. If
          > the url is invalid, or the url is unreachable, the object will throw a
          > WebException. Of course, this doesn't distinguish between a bad URL
          > and a bad network connection, for that you would need some additional
          > tests, perhaps fetching from a known good URL.
          >
          > HTH,
          >
          > --
          > Scott
          > http://www.OdeToCode.com
          >
          > On Tue, 27 Apr 2004 11:46:53 +0800, "Henry Jia" <jia_h@hotmail. com>
          > wrote:
          >[color=green]
          > >Hi,
          > >
          > > Does there has any easy way to check if a specified url is a valid one (both the format should be valid but also the specified path / file is available)?
          > >
          > > Thanks in advance!
          > >
          > >Henry[/color][/color]

          --
          Scott

          Comment

          Working...