林存在:youtube网址无效?使用这样的代码,以检查是否网址是真实的,
其优良的工作一般,但它不工作的YouTube网址.. 如此有效的URL:http://www.youtube.com/watch?v=c4IBN5OAdZc
Utils.UrlExists(uri)
public static bool UrlExists(string url)
{
using (var client = new MyClient())
{
client.HeadOnly = true;
// fine, no content downloaded
try
{
string s1 = client.DownloadString(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;
}
}
youtube网址由于某种原因而区分大小写,这可能会对您有所帮助 – Eric 2011-03-01 22:45:03
“不工作”是什么意思?你的代码运行并为我返回期望的回复。 – 2011-03-01 22:48:59
@John ...看起来像Eric是对的。小写字母不起作用。我不知道这甚至是可能的! – raklos 2011-03-01 22:57:19