HttpClient GetStringAsync returns 301,但浏览器能够导航到正确的地址

HttpClient GetStringAsync returns 301, but browsers are able to navigate to correct address

我认为 HttpClient 应该能够处理 301 错误代码,并重定向到正确的地址。但是对于以下地址 (https://www.npr.org/templates/rss/podcast.php?id=510298),它会抛出异常。不过,浏览器能够正确处理它。只是想了解是否有出路还是我错过了什么。

HttpClient client = new HttpClient();

var s = await client
    .GetStringAsync("https://www.npr.org/templates/rss/podcast.php?id=510298");

抛出

Test1 [0:00.779] Failed: System.Net.Http.HttpRequestException : Response 
status code does not indicate success: 301 (Moved Permanently).
System.Net.Http.HttpRequestException : Response status code does not 
indicate success: 301 (Moved Permanently).
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
   at System.Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask)
   at XUnitTestProject1.UnitTest1.Test1() in 
   C:\Users\res\source\repos\test\XUnitTestProject1\UnitTest1.cs:line 13

终于找到原因了。通常 HttpClient 会正确处理重定向,但它不会处理来自 https -> http 的重定向。这里就是这种情况。我被 chrome 浏览器的响应难倒了,在 chrome 中显示的重定向 url 以 https (https://www.npr.org/rss/podcast.php?id=510298), 但实际重定向到 http://www.npr.org/rss/podcast.php?id=510298

https://github.com/dotnet/runtime/issues/21446#issuecomment-298323133

有解释