我正在尝试从Web URL检查是否存在.txt文件。这是我的代码:
static public bool URLExists(string url)
{
bool result = false;
WebRequest webRequest = WebRequest.Create(url);
webRequest.Timeout = 1200; // miliseconds
webRequest.Method = "HEAD";
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)webRequest.GetResponse();
result = true;
}
catch (WebException webException)
{
//(url + " doesn't exist: " + webException.Message);
}
finally
{
if (response != null)
{
response.Close();
}
}
return result;
}
如果我输入的“ http://www.example.com/demo.txt ”不是有效的文件路径,并且网站显示404错误页面,则返回此代码true。如何解决这个问题呢。提前致谢。
一只萌萌小番薯
相关分类