youtube网址无效?

时间:2011-03-01 22:43:09

标签: c#

我正在使用这样的代码检查网址是否真实存在:

它的工作正常,但它不适用于youtube网址.. 例如,这个有效的网址: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 个答案:

答案 0 :(得分:1)

考虑这种方法:

使用您的C#代码点击此URL,并分析响应:

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

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

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

答案 1 :(得分:0)

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