仅当您的“文件夹”是网页或包含默认文档时才有效。
适用于页面,而不是文件夹
public function IsFolderOnline(url as string) as boolean
try
dim wc as new system.net.webclient()
wc.downloadstring(url)
return true
catch ex as exception
return false
end try
end function
像这样使用它:
dim IsItOnline as boolean = IsFolderOnline("http://example.org/folder")
无法获取文件夹内的所有页面
您必须检查每个页面的 href 元素才能找到新的 url。它只会找到有网络链接的网址。
Webclient.Downloadstring() 下载页面的 html。
使用此代码从页面中检索所有 url
' Finding urls in the page
Dim FoundUrls As New List(Of String)
Dim strRegex As String = "<a[^<>]*>"
Dim myRegex As New Text.RegularExpressions.Regex(strRegex, System.Text.RegularExpressions.RegexOptions.None)
For Each myMatch As Text.RegularExpressions.Match In myRegex.Matches(html)
If myMatch.Success Then
Dim strRegex2 As String = "href=""[^""]*"""
Dim myRegex2 As New Text.RegularExpressions.Regex(strRegex, System.Text.RegularExpressions.RegexOptions.None)
For Each myMatch2 As Text.RegularExpressions.Match In myRegex.Matches(myMatch.Value)
If myMatch2.Success Then
Dim value As String = Split(Split(myMatch2.Value, """", 2).Last, """", 2).First
If value.ToLower.StartsWith("http://" & DomainName.ToLower) Then
FoundUrls.Add(value)
End If
If value.ToLower.StartsWith("https://" & DomainName.ToLower) Then
FoundUrls.Add(value)
End If
If value.ToLower.StartsWith("/") Then
FoundUrls.Add(Protocol & DomainName & value)
Else
If Not value.Contains(":") Then
FoundUrls.Add(Protocol & DomainName & "/" & value)
End If
End If
End If
Next
End If
Next
您可能想要删除包含文件的链接
'Remove unsupported urls
Dim UnsupportedExtensions() As String = {".css", ".ico", ".xml", ".axd", ".js", ".zip", ".rar", ".7z", ".tar", ".jar", ".pdf", ".jpg", ".jpeg", ".png", ".bmp", ".mp4", ".mov", ".mpeg", ".gif"}
For Each url In FoundUrls.ToList
For Each UnsupportedExtension In UnsupportedExtensions
If url.ToLower.EndsWith(UnsupportedExtension) Then
FoundUrls.Remove(url)
Exit For
End If
Next
Next