Check whether the remote URL exists:
Private Void Page_load ( Object Sender, system. eventargs E)
{
String Url1 = " Http://dotnet.aspx.cc/ " ;
String Url2 = " Http://dotnet.aspx.cc/Images/logo.gif " ;
Response. Write ( " <Li> Method 1: " );
Response. Write (url1 + " Exist: " + Urlexistsusinghttpwebrequest (url1). tostring ());
Response. Write ( " <Li> Method 2: " );
Response. Write (url1 + " Exist: " + Urlexistsusingsockets (url1). tostring ());
Response. Write ( " <Li> method 3: " );
Response. Write (url1 + " Exist: " + Urlexistsusingxmlhttp (url1). tostring ());
Response. Write ( " <Li> Method 1: " );
Response. Write (url2 + " Exist: " + Urlexistsusinghttpwebrequest (url2). tostring ());
Response. Write ( " <Li> method 3: " );
Response. Write (url2 + " Exist: " + Urlexistsusingxmlhttp (url2). tostring ());
}
Private Bool Urlexistsusinghttpwebrequest ( String URL ){
Try
{
System. net. httpwebrequest myrequest = (System. net. httpwebrequest) system. net. webrequest. Create (URL );
Myrequest. Method = " Head " ;
Myrequest. Timeout = 100 ;
System. net. httpwebresponse res = (System. net. httpwebresponse) myrequest. getresponse ();
Return (Res. statuscode = System. net. httpstatuscode. OK );
}
Catch (System. net. webexception we)
{
System. Diagnostics. Trace. Write (We. Message );
Return False ;
}
}
Private Bool Urlexistsusingxmlhttp ( String URL)
{
// Note: msxml2.dll must be referenced in this method.
Msxml2.xmlhttp _ XMLHTTP = New Msxml2.xmlhttpclass ();
_ XMLHTTP. Open ( " Head " , URL, False , Null , Null );
_ XMLHTTP. Send ( "" );
Return (_ XMLHTTP. Status = 200 );
}
Private Bool Urlexistsusingsockets ( String URL)
{
If (URL. startswith ( " Http :// " ) URL = URL. Remove ( 0 , " Http :// " . Length );
Try
{
System. net. iphostentry iphost = System. net. DNS. Resolve (URL );
Return True ;
}
Catch (System. net. sockets. socketexception SE)
{
System. Diagnostics. Trace. Write (SE. Message );
Return False ;
}
}