vb.net如何判断网站页面是否存在?

mythhack 2011-01-19 05:11:00
vb.net 判断网页返回值
可以通过判断http头来判断吗?
如果网站页面为404则执行..
如果网站页面为200则..
就是不明白vb.net应该怎么判断http头这些希望解答


...全文
590 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2011-01-19
  • 打赏
  • 举报
回复
Dim html As String = ""
Dim request As HttpWebRequest = DirectCast(WebRequest.Create("域名"), HttpWebRequest)
request.Accept = "*/*"
Dim response As HttpWebResponse = Nothing
Dim stream As Stream = Nothing
Dim reader As StreamReader = Nothing
Try
response = DirectCast(request.GetResponse(), HttpWebResponse)
'读取不到发生异常,就是访问不到
Catch excpt As Exception
End Try

Try
Dim client As New WebClient()
Dim buffer As Byte() = client.DownloadData(url)
Catch generatedExceptionName As Exception
isConnect = False
End Try
Try
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(""), HttpWebRequest)
Dim myResponse As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)

If myResponse.StatusDescription.ToString().ToUpper() = "" Then
End If
Catch webExcp As WebException
End Try
mythhack 2011-01-19
  • 打赏
  • 举报
回复
..解决了我没加载出错的那些 但是UrlIsExist这个函数可以判断网页返回值吗?
mythhack 2011-01-19
  • 打赏
  • 举报
回复
会提示未声明“Response”。它可能因其保护级别而不可访问
mythhack 2011-01-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xrongzhen 的回复:]
VB.NET code

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 isEx……
[/Quote]
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



xrongzhen 2011-01-19
  • 打赏
  • 举报
回复

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




那一句报的错??
mythhack 2011-01-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 net_lover 的回复:]
判断网络地址 Url 是否存在的方法

http://dotnet.aspx.cc/file/Delect-Url-Exists.aspx

你转成VB.NET就可以了,网上有免费的工具
http://www.developerfusion.com/tools/convert/csharp-to-vb/
[/Quote]
转换完会提示
未声明“Response”。它可能因其保护级别而不可访问。
孟子E章 2011-01-19
  • 打赏
  • 举报
回复
判断网络地址 Url 是否存在的方法

http://dotnet.aspx.cc/file/Delect-Url-Exists.aspx

你转成VB.NET就可以了,网上有免费的工具
http://www.developerfusion.com/tools/convert/csharp-to-vb/

16,721

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧