C#(.NET6)中是否有类似于Python response.history的东西



在C#及其标头中生成最后一个响应之前,我需要获得一个响应。

我可以在python中使用:

response = requests.post(f"{uri}")
response.history #List

在C#中,我目前正在使用HttpClient进行POST请求,但我只能使用
var responseString = await response.Content.ReadAsStringAsync();

var responseHeader = response.Headers;获得最终响应

您可以在HttpClient中禁用自动重定向,如下所示:

HttpClientHandler httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false};
HttpClient httpClient = new HttpClient(httpClientHandler);
// Use httpClient. It will not automatically redirect

因此,您必须手动检查响应中的状态代码,并在必要时重定向,但可以通过这种方式跟踪重定向。

最新更新