URLが存在するかどうかを確認する方法は?

[ad_1]

VB
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)

私が試したこと:

このループは 2 後に停止します。なぜですか? 今、私はこれで何がうまくいかなかったのか理解していません

解決策 1

これでうまくいくはずです:

VB
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

[ad_2]

Source link

コメント

タイトルとURLをコピーしました