2011-12-19 145 views
2

我試圖從vimeo下載視頻)與c#。 問題是,當我必須接收代碼302來執行重定向時,我收到代碼200(帶有「Permision denied」的頁面)。 這是代碼:從vimeo下載視頻

string url = "http://player.vimeo.com/play_redirect?clip_id=" + clip_id + "&sig=" + request_signature + "&time=" + request_signature_expires + "&quality=" + hd + "&codecs=H264,VP8,VP6&type=moogaloop_local&embed_location="; 

_VideoURL = GetHttpSpecial(url, cookies); 

public string GetHttpSpecial(string url, CookieContainer cookies) 
     { 
      string html = ""; 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
      request.Method = "GET"; 
      request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; 
      request.Headers.Add("Accept-Language: nl,en-us;q=0.7,en;q=0.3"); 
      request.Headers.Add("Accept-Encoding: gzip, deflate"); 
      request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0"; 
      request.KeepAlive = true; 
      request.Timeout = 20000; 
      request.AllowAutoRedirect = false; 
      request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
      foreach (Cookie cookie in response.Cookies) 
      { 
       cookies.Add(cookie); 
      } 
      if (response.StatusCode == HttpStatusCode.OK) 
      { 
       try 
       { 
        Stream responseStream = response.GetResponseStream(); 
        StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); 
        try 
        { 
         html = reader.ReadToEnd(); 
        } 
        finally 
        { 
         reader.Close(); 
        } 
       } 
       finally 
       { 
        response.Close(); 
       } 
       return html; 
      } 
      else 
      { 
       if (response.StatusCode == HttpStatusCode.Found) 
       { 
        return response.Headers["Location"].ToString(); 
       } 
       else 
       { 
        return "error: server returned status description:" + response.StatusDescription; 
       } 
      } 
     } 

請問您能幫我嗎?

感謝

+0

嗨薩拉,歡迎來到這個網站。當你以編程方式嘗試下載視頻或僅通過網站使用vimeo時,你是否看到了這一點? – StuperUser 2011-12-19 12:18:52

+0

我試圖用我的應用程序編程從vimeo下載視頻 – 2011-12-19 12:23:08

回答

0

薩拉,使用Fiddler檢查請求/響應,以確保您發送相同的。當我觀看通常使用網站播放視頻時,會有一個vimeo cookie向後傳遞,而不是在您直接調用播放鏈接時傳遞的。

也許嘗試莊家防範主頁首先從Repsonse.GetResponseHeader("Set-Cookie")獲得VIMEO餅乾(和使用,在您的進一步調用的請求。

這可能是Vimeo的不允許那些沒有被提到的請求從他們的網站,所以你也可能需要做一個

request.Referer = 
      "http://a.vimeocdn.com/[[IMPORT]]/player.vimeo.com/assets/flash/moogaloop/5.2.8/controllers/videoController.swf"; 
+0

我用小提琴檢查過它,請求/響應是:

 # \t Result \t Protocol \t Host \t URL \t Body \t Caching \t Content-Type \t Process \t Comments \t Custom \t 118 \t 302 \t HTTP \t player.vimeo.com \t /play_redirect?clip_id=33735037&sig=85b60dd8d19ec06b9307fbc1e733048d&time=1324303943&quality=hd&codecs=H264,VP8,VP6&type=moogaloop_local&embed_location= \t 20 \t no-store, no-cache, must-revalidate Expires: Fri, 25 Feb 1983 09:30:00 GMT \t text/html; charset=UTF-8 \t avp:3544 \t \t \t 119 \t 200 \t HTTP \t av.vimeo.com \t /38970/772/76932486.mp4?token=1324304859_f553bbbfb98f07551487cefe95ec3135 \t 55.608.510 \t \t video/mp4 \t avp:3544 \t \t \t 
2011-12-19 14:22:28

+0

用我在c#中的代碼,我從來沒有收到302響應。我收到了Permision否認的200碼。 我嘗試了一個引用程序,但我無法重定向。 謝謝 – 2011-12-19 14:22:40

1

此代碼對我的作品 我創造了新的WPF項目,簡單的按鈕下面是後臺代碼:。

private Dictionary<int, CookieContainer> m_cookieContainer = new Dictionary<int, CookieContainer>(); 

private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      HttpWebRequest req = CreateRequest("http://vimeo.com/42082443"); 
      req.AllowAutoRedirect = false; 
      req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0"; 
      req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; 
      req.Headers[HttpRequestHeader.AcceptLanguage] = "ru,en;q=0.8,en-us;q=0.5,uk;q=0.3"; 
      req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate"; 
      req.KeepAlive = true; 
      req.Timeout = 20000; 
      req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; 
      using (HttpWebResponse response = GetResponse(req)) 
      { 
       if (response.StatusCode == HttpStatusCode.OK) 
       { 
        string pageData = new StreamReader(response.GetResponseStream()).ReadToEnd(); 

        string clipId = null; 
        if (Regex.Match(pageData, @"clip_id=(\d+)", RegexOptions.Singleline).Success) 
        { 
         clipId = Regex.Match(pageData, @"clip_id=(\d+)", RegexOptions.Singleline).Groups[1].ToString(); 
        } 
        else if (Regex.Match(pageData, @"(\d+)", RegexOptions.Singleline).Success) 
        { 
         clipId = Regex.Match(pageData, @"(\d+)", RegexOptions.Singleline).Groups[1].ToString(); 
        } 

        string sig = Regex.Match(pageData, "\"signature\":\"(.+?)\"", RegexOptions.Singleline).Groups[1].ToString(); 
        string timestamp = Regex.Match(pageData, "\"timestamp\":(\\d+)", RegexOptions.Singleline).Groups[1].ToString(); 

        string videoUrl = string.Format("http://player.vimeo.com/play_redirect?clip_id={0}&sig={1}&time={2}&quality=hd&codecs=H264,VP8,VP6&type=moogaloop_local&embed_location=", clipId, sig, timestamp); 

        req = CreateRequest(videoUrl); 
        req.AllowAutoRedirect = false; 
        req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0"; 
        req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; 
        req.Headers[HttpRequestHeader.AcceptLanguage] = "ru,en;q=0.8,en-us;q=0.5,uk;q=0.3"; 
        req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate"; 
        req.KeepAlive = true; 
        req.Referer = "http://a.vimeocdn.com/p/flash/moogaloop/5.2.25/moogaloop.swf?v=1.0.0"; 
        req.Timeout = 20000; 
        req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; 
        using (HttpWebResponse response2 = GetResponse(req)) 
        { 
         if (response2.StatusCode == HttpStatusCode.Found) 
         { 
          string location = response2.Headers[HttpResponseHeader.Location]; 
          MessageBox.Show(location); 
         } 
        } 
       } 
      } 
     } 

     private CookieContainer GetCookieContainerPerThread() 
     { 
      int managedThreadId = Thread.CurrentThread.ManagedThreadId; 
      lock (typeof(MainWindow)) 
      { 
       if (!this.m_cookieContainer.ContainsKey(managedThreadId)) 
       { 
        CookieContainer container = new CookieContainer(); 
        this.m_cookieContainer.Add(managedThreadId, container); 
       } 
      } 
      return this.m_cookieContainer[managedThreadId]; 
     } 

     public HttpWebRequest CreateRequest(string url) 
     { 
      HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); 
      req.CookieContainer = this.GetCookieContainerPerThread(); 
      //this.InitProxy(req); 
      return req; 
     } 

     public HttpWebResponse GetResponse(HttpWebRequest req) 
     { 
      HttpWebResponse response = (HttpWebResponse)req.GetResponse(); 
      this.GetCookieContainerPerThread().Add(response.Cookies); 
      return response; 
     } 
+0

我試過了你的答案,但我只能看到「位置」:我該如何使用該位置下載視頻?謝謝 – Marco 2013-03-10 11:31:16

+0

@Marco Location應該是直接鏈接到遠程服務器上的物理視頻文件。將它傳遞給WebClient.DownloadFile – 2013-03-10 16:29:56

+0

嗯,它適用於您使用的視頻鏈接。但是,如果我嘗試使用[this](http://vimeo.com/30881879)或[this other](http://vimeo.com/17913746)位置爲null。你有什麼主意嗎?非常感謝你 – Marco 2013-03-11 07:52:37