如何检查网址是否存在? [英] How to check url exist or not?
本文介绍了如何检查网址是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Dim urlexist As 整数 = 0
Dim urlDoesNotExist 作为 整数 = 0
对于 每个行在 TextBox1.Text.Split(Environment.NewLine)
Dim url 作为 新系统.Uri(line)
Dim req As System.Net.WebRequest
req = System.Net.WebRequest.Create(url)
Dim resp As System.Net。 WebResponse
尝试
resp = req.GetResponse()
resp.Close()
req = 没什么
urlexist = urlexist + 1
' MsgBox(找到网站!)
Catch ex As Exception
req = Nothing
urlDoesNotExist = urlDoesNotExist + 1
MsgBox(未找到网站。检查网址和互联网连接)
Button1.Text = urlDoesNotExist + 1
返回
结束 尝试
下一步
MsgBox( 存在是,urlexist)
MsgBox( 不存在是,urlDoesNotExist)
我的尝试:
此循环在2之后停止为什么?现在我不知道这里出现了什么问题
解决方案
这应该可以解决问题:
私有 功能 CheckUrl(uri As Uri)作为Boolean
Dim request As HttpWebRequest = DirectCast (WebRequest.Create(uri),HttpWebRequest)
request.Method = WebRequestMethods.Http.Head
Dim 响应作为 HttpWebResponse = DirectCast (request.GetResponse(),HttpWebResponse)
返回 response.StatusCode = HttpStatusCode.OK
结束 功能跨度>
Dim urlexist As Integer = 0
Dim urlDoesNotExist As Integer = 0
For Each line In TextBox1.Text.Split(Environment.NewLine)
Dim url As New System.Uri(line)
Dim req As System.Net.WebRequest
req = System.Net.WebRequest.Create(url)
Dim resp As System.Net.WebResponse
Try
resp = req.GetResponse()
resp.Close()
req = Nothing
urlexist = urlexist + 1
'MsgBox("Website Found!")
Catch ex As Exception
req = Nothing
urlDoesNotExist = urlDoesNotExist + 1
' MsgBox("Website not found. Check the url and internet connection")
Button1.Text = urlDoesNotExist + 1
Return
End Try
Next
MsgBox("exist is ", urlexist)
MsgBox("non-exist is ", urlDoesNotExist)
What I have tried:
this loop stops after 2 why ? now i don't under stand what went wrong in this
解决方案
This should do the trick:
Private Function CheckUrl(uri As Uri) As Boolean Dim request As HttpWebRequest = DirectCast(WebRequest.Create(uri), HttpWebRequest) request.Method = WebRequestMethods.Http.Head Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse) Return response.StatusCode = HttpStatusCode.OK End Function
这篇关于如何检查网址是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文