有没有一种通过Visual Basic检查URL语法的方法?以下是我的代码。我需要一种只检查语法并确保其正确(即具有http,.com或.net或.edu)的方法。我需要检查格式以确保这些网址符合标准网址格式。这可以完成吗?
Public PageSource As String
Public httpRequest As Object
Function GetURLStatus(ByVal URL As String, Optional AllowRedirects As Boolean)
Const WinHttpRequestOption_EnableRedirects = 6
If httpRequest Is Nothing Then
On Error Resume Next
Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
If httpRequest Is Nothing Then
Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5")
End If
Err.Clear
On Error GoTo 0
End If
httpRequest.Option(WinHttpRequestOption_EnableRedirects) = AllowRedirects
If InStr(1, URL, "://") = 0 Then
URL = "http://" & URL
End If
On Error Resume Next
httpRequest.Open "GET", URL, False
If Err.Number <> 0 Then
' Handle connection errors
GetURLStatus = Err.Description
Err.Clear
Exit Function
End If
On Error GoTo 0
On Error Resume Next
httpRequest.Send
httpRequest.WaitForResponse
If Err.Number <> 0 Then
PageSource = "Error"
GetURLStatus = Err.Description
Err.Clear
Else
GetURLStatus = httpRequest.Status & " - " & httpRequest.StatusText
PageSource = httpRequest.ResponseText
End If
On Error GoTo 0
End Function