我正在尝试使用c#从vimeo下载视频。
问题是当我必须接收解码器302进行重定向时,我会收到代码200(页面上显示“拒绝拒绝”)。
这是代码:
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;
}
}
}
请问你能帮帮我吗?
谢谢
最佳答案
该代码对我有效。
我用简单的按钮创建了新的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;
}
关于c# - 从vimeo下载视频,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8561199/