<分区>
我有以下方法返回给定 Url
的 Http 状态代码
:
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
?