validate an url

Discussion in 'ASP .Net' started by Guoqi Zheng, Oct 8, 2004.

  1. Guoqi Zheng

    Guoqi Zheng Guest

    How can I validate an URL in real time, connect to that URL directly and see
    it response with ok status.

    I think it might need to use webclient, or webrequest + webresponse, but I
    have not found out how to do it.

    Any help?

    --
    Kind regards

    Guoqi Zheng
    guoqi AT meetholland dot com
    Http://www.meetholland.com
    Guoqi Zheng, Oct 8, 2004
    #1
    1. Advertising

  2. Guoqi Zheng

    Scott Allen Guest

    You can use either WebClient or WebRequest. I have an article using
    both classes that does more than you need, but should give you more
    than enough code to get started:

    http://odetocode.com/Articles/162.aspx

    HTH,

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Sat, 9 Oct 2004 00:00:07 +0200, "Guoqi Zheng" <> wrote:

    >How can I validate an URL in real time, connect to that URL directly and see
    >it response with ok status.
    >
    >I think it might need to use webclient, or webrequest + webresponse, but I
    >have not found out how to do it.
    >
    >Any help?
    Scott Allen, Oct 9, 2004
    #2
    1. Advertising

  3. Guoqi Zheng

    jongalloway Guest

    Guoqi Zheng wrote:
    > How can I validate an URL in real time, connect to that URL directly

    and see
    > it response with ok status.
    >
    > I think it might need to use webclient, or webrequest + webresponse,

    but I
    > have not found out how to do it.
    >
    > Any help?
    >
    > --
    > Kind regards
    >
    > Guoqi Zheng
    > guoqi AT meetholland dot com
    > Http://www.meetholland.com
    jongalloway, Oct 9, 2004
    #3
  4. Guoqi Zheng

    jongalloway Guest

    Guoqi -

    Check for HttpWebResponse.StatusCode == HttpStatusCode.OK.

    Sample function from MSDN
    (http://msdn.microsoft.com/library/d...temnethttpwebresponseclassstatuscodetopic.asp)

    public static void GetPage(String url)
    {
    try
    {
    // Creates an HttpWebRequest for the specified URL.
    HttpWebRequest httpWebRequest =
    (HttpWebRequest)WebRequest.Create(url);
    // Sends the HttpWebRequest and waits for a response.
    HttpWebResponse httpWebResponse =
    (HttpWebResponse)httpWebRequest.GetResponse();
    if (httpWebResponse.StatusCode == HttpStatusCode.OK)
    Console.WriteLine("\r\nResponse Status Code is OK and
    StatusDescription is: {0}",
    httpWebResponse.StatusDescription);
    // Releases the resources of the response.
    httpWebResponse.Close();

    }
    catch(WebException e)
    {
    Console.WriteLine("\r\nWebException Raised. The following
    error occured : {0}",e.Status);
    }
    catch(Exception e)
    {
    Console.WriteLine("\nThe following Exception was raised :
    {0}",e.Message);
    }
    }

    - Jon
    http://weblogs.asp.net/jgalloway

    Guoqi Zheng wrote:
    > How can I validate an URL in real time, connect to that URL directly

    and see
    > it response with ok status.
    >
    > I think it might need to use webclient, or webrequest + webresponse,

    but I
    > have not found out how to do it.
    >
    > Any help?
    >
    > --
    > Kind regards
    >
    > Guoqi Zheng
    > guoqi AT meetholland dot com
    > Http://www.meetholland.com
    jongalloway, Oct 9, 2004
    #4
  5. Guoqi Zheng

    jongalloway Guest

    Guoqi -

    You need to check the HttpWebRequest.Status property. Sample code from
    MSDN:

    http://msdn.microsoft.com/library/d...temnethttpwebresponseclassstatuscodetopic.asp

    public static void GetPage(String url)
    {
    try
    {
    // Creates an HttpWebRequest for the specified URL.
    HttpWebRequest myHttpWebRequest =
    (HttpWebRequest)WebRequest.Create(url);
    // Sends the HttpWebRequest and waits for a response.
    HttpWebResponse myHttpWebResponse =
    (HttpWebResponse)myHttpWebRequest.GetResponse();
    if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
    Console.WriteLine("\r\nResponse Status Code is OK and
    StatusDescription is: {0}",

    myHttpWebResponse.StatusDescription);
    // Releases the resources of the response.
    myHttpWebResponse.Close();

    }
    catch(WebException e)
    {
    Console.WriteLine("\r\nWebException Raised. The following
    error occured : {0}",e.Status);
    }
    catch(Exception e)
    {
    Console.WriteLine("\nThe following Exception was raised :
    {0}",e.Message);
    }
    }

    - Jon
    http://weblogs.asp.net/jgalloway
    jongalloway, Oct 10, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug
    Replies:
    0
    Views:
    458
  2. Jon paugh
    Replies:
    1
    Views:
    679
  3. ALI-R

    How to validate a URL

    ALI-R, Nov 12, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    7,889
    Mark Rae
    Nov 12, 2004
  4. franz

    regEx to validate a web url

    franz, Sep 14, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,985
    Joerg Jooss
    Sep 14, 2005
  5. Just D.
    Replies:
    0
    Views:
    398
    Just D.
    Aug 11, 2004
Loading...

Share This Page