判断URL的HTTP状态

代码
Public Class TestExist
Public Shared Function UrlExist(ByVal sURL As String) As Boolean
Dim temp As Boolean = False

Try
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(sURL), HttpWebRequest)
'WebProxy proxy = new WebProxy("your proxy server", 8080);
'request.Proxy = proxy;
request.Method = "HEAD"
request.AllowAutoRedirect
= False
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)


Select Case response.StatusCode
Case HttpStatusCode.OK
temp
= True
Exit Select
Case HttpStatusCode.Moved
temp
= True
Exit Select
Case HttpStatusCode.NotFound
temp
= False
Exit Select
Case HttpStatusCode.MultipleChoices
temp
= True
Exit Select
Case HttpStatusCode.Ambiguous
temp
= True
Exit Select
Case HttpStatusCode.MovedPermanently
temp
= True
Exit Select
Case HttpStatusCode.Moved
temp
= True
Exit Select
Case HttpStatusCode.Found
temp
= True
Exit Select
Case HttpStatusCode.Redirect
temp
= True
Exit Select
Case HttpStatusCode.SeeOther
temp
= True
Exit Select
Case HttpStatusCode.RedirectMethod
temp
= True
Exit Select
Case HttpStatusCode.NotModified
temp
= True
Exit Select
Case HttpStatusCode.UseProxy
temp
= True
Exit Select
Case HttpStatusCode.Unused
temp
= True
Exit Select
Case HttpStatusCode.TemporaryRedirect
temp
= True
Exit Select
Case HttpStatusCode.RedirectKeepVerb
temp
= True
Exit Select
Case Else
temp
= False
End Select
response.Close()
Catch ex As Exception
temp
= False
End Try

Return temp
End Function
End Class

 

posted @ 2010-01-09 21:05  大力  阅读(467)  评论(0编辑  收藏  举报