首页 > 解决方案 > 绕过 308 永久重定向

问题描述

我正在尝试使用一些 vb .net 代码从 URL 下载内容

Dim httpClientHandler = New HttpClientHandler
With httpClientHandler
    .AllowAutoRedirect = True
End With
Using request = New HttpRequestMessage(HttpMethod.[Get], New Uri(url))
    request.Headers.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml")
    request.Headers.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate")
    request.Headers.TryAddWithoutValidation("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36")
    request.Headers.TryAddWithoutValidation("connection", "keep-alive")
    request.Headers.TryAddWithoutValidation("Accept-Charset", "ISO-8859-1")
    Dim httpClient = New HttpClient(httpClientHandler)
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 Or SecurityProtocolType.Tls12 Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls

    Using response = Await httpClient.SendAsync(request).ConfigureAwait(False)
        response.EnsureSuccessStatusCode()

        Using responseStream = Await response.Content.ReadAsStreamAsync().ConfigureAwait(False)

            Using decompressedStream = New GZipStream(responseStream, CompressionMode.Decompress)

                Using streamReader = New StreamReader(decompressedStream)
                    Return Await streamReader.ReadToEndAsync().ConfigureAwait(False)
                End Using
            End Using
        End Using
    End Using
End Using

从最近开始,这开始返回 308 - 永久重定向。我尝试的一件事是添加 .AllowAutoRedirect,但它没有帮助。此外,我已经看到一些答案,建议尝试在响应的 Location 标头中 ping URL,但在我的情况下,location 标头与我首先尝试 ping 的 URL 相同,所以它是一个循环。

有没有办法绕过 308 重定向,并以某种方式获取内容?正如预期的那样,在浏览器中,URL 可以正常工作。

标签: vb.netredirecthttprequesthttpclienthttp-status-code-308

解决方案


推荐阅读