gpt4 book ai didi

c# - 如何发现给定 HTTP 请求失败的原因?

转载 作者:行者123 更新时间:2023-11-30 22:19:54 25 4
gpt4 key购买 nike

我想找出导致链接失效的原因。该链接应该显示一条特定的消息,而不是不起作用,例如 404 或 403。我如何才能发现导致给定请求失败的 HTTP 状态?

if (!IsLinkWorking(link))
{
//Here you can show the error. You don't specify how you want to show it.
TextBox2.ForeColor = System.Drawing.Color.Green;
TextBox2.Text += string.Format("{0}\nNot working\n\n ", link);
}
else
{
TextBox2.Text += string.Format("{0}\n working\n\n", link);
}

最佳答案

您需要使用 HttpWebRequest。这将返回一个 HttpWebResponse,它有一个 StatusCode 属性 - see the documentation here .

这是一个例子:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK) {
TextBox2.Text = "HTTP Response is: {0}", response.StatusDescription);
}

关于c# - 如何发现给定 HTTP 请求失败的原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15307894/

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