if (IsURLValid(url))
{
Uri requestUri = new Uri(url);
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri);
httpWebRequest.Proxy = WebRequest.GetSystemWebProxy();
httpWebRequest.Timeout = 3000;
httpWebRequest.Method = "HEAD";
//httpWebRequest.AllowAutoRedirect = true;
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0";
httpWebRequest.Accept = "*/*";
httpWebRequest.ServicePoint.Expect100Continue = false;
httpWebRequest.AllowAutoRedirect = true;
httpWebRequest.MaximumAutomaticRedirections = 4;
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
httpWebResponse.Close();
MessageBox.Show(httpWebResponse.ResponseUri.ToString());
if ((httpWebResponse.StatusCode == HttpStatusCode.OK) || (httpWebResponse.StatusCode == HttpStatusCode.Redirect))
{
return true;
}
else
{
return false;
}
}
return false;
}
catch (WebException)
{
return false;
}
}
上面的代码运行良好,但是给出了错误的结果。下面的网址是实时的,但是我们得到的结果无效。
我也尝试了在线检查程序,这些检查程序也给出了相同的错误。 https://httpstatus.io/ 检查httpstatus中的统一网址,提供错误。有人知道为什么会这样吗?
答案 0 :(得分:0)
您可以尝试以下方法:
httpWebRequest.CookieContainer = new CookieContainer();
否则,您的(HttpWebResponse)httpWebRequest.GetResponse();
将引发异常,并且您将从catch语句中获取return false;
。