如何在 C# 中使用 HttpClient 获取所有重定向 URL 的列表

How to get a list of all redirected URLs using HttpClient in C#

是否有来自 Java 的类似内容:

对于 C# HttpClient?

您可以使用 HttpWebRequest:

public static string GetRedirectedUrls(string url)
{
    StringBuilder sb = new StringBuilder();
    while (!string.IsNullOrWhiteSpace(url))
    {
        sb.AppendLine(url);
        HttpWebRequest request = HttpWebRequest.CreateHttp(url);
        request.AllowAutoRedirect = false;
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            url = response.GetResponseHeader("Location");
        }
    }

    return sb.ToString();
}