validate an url

G

Guoqi Zheng

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
 
J

jongalloway

Guoqi said:
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
 
J

jongalloway

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
 
J

jongalloway

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top