How to check whether a remote URL exists

Source: Internet
Author: User
Tags dotnet

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 ;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.