reggiepangilinanX

is there a way to check if a url or a website exists



Re: .NET Framework Networking and Communication check if a url exists

timvw

Use HttpWebRequest and check the statusCode in the HttpWebResponse... (A HEAD instead of a GET request is sufficient to do this)

eg:

Code Snippet

Uri uri = new Uri("http://www.timvw.be");
HttpWebRequest httpWebRequest = WebRequest.Create(uri) as HttpWebRequest;
httpWebRequest.Method = "HEAD";
httpWebRequest.AllowAutoRedirect = true;
// we'll use the Async pattern...
httpWebRequest.BeginGetResponse(this.GetResponseCompleted, new object[] { httpWebRequest, uri });

private void GetResponseCompleted(IAsyncResult ar)
{
object[] objects = ar.AsyncState as object[];
HttpWebRequest httpWebRequest = objects[0] as HttpWebRequest;
Uri uri = objects[1] as Uri;

HttpWebResponse httpWebResponse = null;
int httpStatusCode;

try
{
httpWebResponse = httpWebRequest.EndGetResponse(ar) as HttpWebResponse;
httpStatusCode = (int)httpWebResponse.StatusCode;
}
catch (WebException webException)
{
httpWebResponse = webException.Response as HttpWebResponse;
if (httpWebResponse != null)
{
httpStatusCode = (int)httpWebResponse.StatusCode;
}
}
finally
{

if (httpStatusCode == 200 ) { Console.WriteLine("the resource exists"); }

if (httpWebResponse != null)
{
httpWebResponse.Close();
}
}
}

}






Re: .NET Framework Networking and Communication check if a url exists

reggiepangilinanX

i've tried your code and it returns an exception

"Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy service is denied. )"

what is the meaning of this





Re: .NET Framework Networking and Communication check if a url exists

element109

Code Snippet

'This code attempts to get a response from the passed URL.

Friend Function WebSiteIsAvailable(ByVal linkText As String) As Boolean

Dim URL_Object As New System.Uri(linkText)

Dim URL_WebRequest As System.Net.WebRequest

Dim URL_WebResponse As System.Net.WebResponse

Dim Response_Result As Boolean

Try

URL_WebRequest = System.Net.WebRequest.Create(URL_Object)

URL_WebResponse = URL_WebRequest.GetResponse

Response_Result = True

Catch

Response_Result = False

End Try

URL_WebResponse = Nothing

URL_WebRequest = Nothing

URL_Object = Nothing

Return Response_Result

End Function





Re: .NET Framework Networking and Communication check if a url exists

timvw

reggiepangilinanX wrote:
"Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy service is denied. )"

what is the meaning of this



It means that you have a proxy server in between that requires you to authenticate.. In most situations it's sufficient add the DefaultNetworkCredentials to the httpWebRequest instance before you perform the request:

Code Snippet

httpWebRequest.Credentials = CredentialCache.DefaultNetworkCredentials;
httpWebRequest.GetResponse();