url checker VBA, when redirected, show redirected url

前端 未结 1 1589
误落风尘
误落风尘 2021-01-19 11:40

I\'m quite new to EXCEL VBA\'s and I\'m kinda stuck finding a way to create a MACRO that shows whether a url is still active (200 ok), or may be redirected, and if so, I wa

1条回答
  •  孤城傲影
    2021-01-19 12:05

    Its better to use the WinHttp COM object. That will let you "disable" redirect handling. Read this forum post. The component you need to reference is Microsoft WinHTTP Services.

    Public Function GetResult(ByVal strUrl As String, Optional ByRef isRedirect As Boolean, Optional ByRef target As String) As String
        Dim oHttp As New WinHttp.WinHttpRequest
    
        oHttp.Option(WinHttpRequestOption_EnableRedirects) = False
        oHttp.Open "HEAD", strUrl, False
        oHttp.send
        GetResult = oHttp.Status & " " & oHttp.statusText
        If oHttp.Status = 301 Or oHttp.Status = 302 Then
            isRedirect = True
            target = oHttp.getResponseHeader("Location")
        Else
            isRedirect = False
            target = Nothing
        End If
    End Function
    

    0 讨论(0)
提交回复
热议问题