16,721
社区成员




Private Function UrlIsExist(url As [String]) As Boolean
Dim u As System.Uri = Nothing
Try
u = New Uri(url)
Catch
Return False
End Try
Dim isExist As Boolean = False
Dim r As System.Net.HttpWebRequest = TryCast(System.Net.HttpWebRequest.Create(u), System.Net.HttpWebRequest)
r.Method = "HEAD"
Try
Dim s As System.Net.HttpWebResponse = TryCast(r.GetResponse(), System.Net.HttpWebResponse)
If s.StatusCode = System.Net.HttpStatusCode.OK Then
isExist = True
End If
Catch x As System.Net.WebException
Try
isExist = (TryCast(x.Response, System.Net.HttpWebResponse).StatusCode <> System.Net.HttpStatusCode.NotFound)
Catch
isExist = (x.Status = System.Net.WebExceptionStatus.Success)
End Try
End Try
Return isExist
End Function
Protected Sub Page_Load(sender As Object, e As EventArgs)
Response.Write("<li>http://dotnet.aspx.cc/Images/ 是否存在:" & UrlIsExist("http://dotnet.aspx.cc/Images/"))
Response.Write("<li>http://dotnet.aspx.cc/ImagesX/ 是否存在:" & UrlIsExist("http://dotnet.aspx.cc/ImagesX/"))
Response.Write("<li>http://xxxxx/ 是否存在:" & UrlIsExist("http://xxxxx/"))
Response.Write("<li>hxxxxxxxxxxxxxxxxxxx 是否存在:" & UrlIsExist("hxxxxxxxxxxxxxxxxxxx"))
Response.Write("<li>http://dotnet.aspx.cc/Images/logoSite.gif 是否存在:" & UrlIsExist("http://dotnet.aspx.cc/Images/logoSite.gif"))
Response.Write("<li>http://dotnet.aspx.cc/Images/logoSite2.gif 是否存在:" & UrlIsExist("http://dotnet.aspx.cc/Images/logoSite2.gif"))
End Sub