【发布时间】:2012-11-16 07:12:59
【问题描述】:
我无法检查网址是否存在。 我使用下面的方法进行大多数检查 - 这是我找到的最好的方法,可以保存完整的网络请求,但它确实允许检查地址,例如:
m.bbc.co.uk
m. 的任何移动网站,都没有效果并中断。
public static bool Does_URL_Exists(string str_url)
{
// using MyClient from linked post
using (var client = new MyClient())
{
client.HeadOnly = true;
// fine, no content downloaded
try
{
//System.Windows.Forms.MessageBox.Show(str_url);
string s1 = client.DownloadString(str_url);
return true;
}
catch
{
return false;
}
}
}
class MyClient : WebClient
{
public bool HeadOnly { get; set; }
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest req = base.GetWebRequest(address);
if (HeadOnly && req.Method == "GET")
{
req.Method = "HEAD";
}
return req;
}
}
关于我如何完成这项工作的任何线索。 www.bbc.co.uk/m 也不好。
【问题讨论】:
标签: c# web httpwebrequest exists