2011-03-01 135 views
0

林存在: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; 
      } 
     } 
+2

youtube网址由于某种原因而区分大小写,这可能会对您有所帮助 – Eric 2011-03-01 22:45:03

+2

“不工作”是什么意思?你的代码运行并为我返回期望的回复。 – 2011-03-01 22:48:59

+0

@John ...看起来像Eric是对的。小写字母不起作用。我不知道这甚至是可能的! – raklos 2011-03-01 22:57:19

回答

0

erics评论是答案。如果他提出了答案,我会将其标记为正确。在此期间,我将其标记为正确

1

考虑这个做法:

打这个网址与你的C#代码,并分析响应:

http://gdata.youtube.com/feeds/api/videos/<your ID> 

成功后,您将能够看到200的HTTP响应。

在您的浏览器中尝试;你会看到好的ID给你的内容,其他人将返回明文“私人视频”或“无效的ID”。