gpt4 book ai didi

.net - 如何检查302响应? Web请求

转载 作者:行者123 更新时间:2023-12-03 13:47:38 25 4
gpt4 key购买 nike

我想使用WebRequest知道是否


“ 302暂时移动”


响应,而不是自动获取新的网址。

最佳答案

如果要检测重定向响应,则可以自动创建WebRequest并将AllowAutoRedirect属性设置为false而不是遵循它:

HttpWebRequest request = WebRequest.Create(someUrl) as HttpWebRequest;
request.AllowAutoRedirect = false;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.Redirect ||
response.StatusCode == HttpStatusCode.MovedPermanently)
{
// Do something here...
string newUrl = response.Headers["Location"];
}

关于.net - 如何检查302响应? Web请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1391373/

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