我需要执行一个HTTP GET操作并跟随重定向。
using (var client = new HttpClient())
{
var response = await client.GetAsync("https://example.com");
}
问题在于如果服务器返回一个302 HTTP代码重定向到
http://...
(而不是https
),.NET不会遵循它(出于安全考虑)。
我如何强制HttpClient
从HTTPS跟随重定向到HTTP?
我需要执行一个HTTP GET操作并跟随重定向。
using (var client = new HttpClient())
{
var response = await client.GetAsync("https://example.com");
}
http://...
(而不是https
),.NET不会遵循它(出于安全考虑)。
我如何强制HttpClient
从HTTPS跟随重定向到HTTP?
您可能需要额外做一些工作:
using var client = new HttpClient();
var response = await client.GetAsync("https://example.com");
if (new[] {HttpStatusCode.Moved, HttpStatusCode.MovedPermanently}.Contains(response.StatusCode))
{
response = await client.GetAsync(response.Headers.Location);
}
// more code