gpt4 book ai didi

c# - 使用 HttpClient,在 301 的情况下如何防止自动重定向并获取原始状态代码和转发 Url

转载 作者:IT王子 更新时间:2023-10-29 04:07:54 27 4
gpt4 key购买 nike

<分区>

我有以下方法返回给定 UrlHttp 状态代码:

public static async void makeRequest(int row, string url)
{
string result;
Stopwatch sw = new Stopwatch(); sw.Start();

try
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = new HttpResponseMessage();
response = await client.GetAsync(url);

// dump contents of header
Console.WriteLine(response.Headers.ToString());

if (response.IsSuccessStatusCode)
{
result = ((int)response.StatusCode).ToString();
}
else
{
result = ((int)response.StatusCode).ToString();
}
}
}
catch (HttpRequestException hre)
{
result = "Server unreachable";
}

sw.Stop();
long time = sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));

requestComplete(row, url, result, time);
}

它适用于 200/404 等,但是对于 301 代码,我相信返回的结果是 already-redirected (200) 结果,而不是应该返回的实际 301,它会有一个包含重定向指向的 header 。

我在其他 .Net Web 请求类中看到过类似的东西,其中的技术是将某种 allowAutoRedirect 属性设置为 false。如果这是正确的,谁能告诉我 HttpClient 类的正确替代方案?

This post has info on the above allowAutoRedirect concept I mean

否则,对于我知道是真正的 301 的 Urls,我如何才能让此方法返回 301s 而不是 200s

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com