WebRequest request = WebRequest.Create(new Uri("http://istr03.izlesene.com/data/videos/7213/7213704-360_2-103k.mp4/e5ae7a74a30f645c6fc0c5aa0fde9076/531B29B7"));
request.Method = "HEAD";
using (WebResponse response = request.GetResponse()) {
double boyut = response.ContentLength / 1024 / 1024;
string tip = response.ContentType;
}
此代码适用于其他网站,但对于此网址,会引发异常; (500)内部服务器错误。
代码对此网址失败可能有什么不同?
答案 0 :(得分:1)
您需要设置User-Agent http header。这个特定的服务器希望可能会嗅探客户端的功能。
调整代码以获取HttpWebRequest并设置UserAgent属性:
var request = (HttpWebRequest) WebRequest.Create(
new Uri("http://your url here"));
request.Method = "HEAD";
request.UserAgent = "spider/1.0";
using (WebResponse response = request.GetResponse())
{
double boyut = response.ContentLength / 1024 / 1024;
string tip = response.ContentType;
}
您网址的内容长度为:13381446
内容类型是:video / mp4
答案 1 :(得分:1)
我确实用fiddler看看发生了什么。您提供的网址已重定向到另一个URL。
HEAD http://istr03.izlesene.com/data/videos/7213/7213704-360_2-103k.mp4/e5ae7a74a30f645c6fc0c5aa0fde9076/531B29B7 HTTP/1.1
User-Agent: Fiddler
Host: istr03.izlesene.com
HTTP/1.1 302 Moved Temporarily
Via: 1.1 81.212.99.207 (McAfee Web Gateway 7.3.2.3.0.16052)
Date: Fri, 07 Mar 2014 14:45:20 GMT
Server: nginx/1.4.4
Location: http://sstr06.izlesene.com/data/videos/7213/7213704-360_2-103k.mp4?token=WCk3CCIxJYVC0ESMOs1cFw&ts=1394210720
Connection: Keep-Alive
Content-Type: text/html
Content-Length: 160
因此,您需要从第一个响应中解析位置,并使用HEAD方法向服务器执行另一个请求,您将获得视频的内容长度。
HEAD http://sstr06.izlesene.com/data/videos/7213/7213704-360_2-103k.mp4?token=WCk3CCIxJYVC0ESMOs1cFw&ts=1394210720 HTTP/1.1
User-Agent: Fiddler
Host: sstr06.izlesene.com
HTTP/1.1 200 OK
Via: 1.1 81.212.99.207 (McAfee Web Gateway 7.3.2.3.0.16052)
Date: Fri, 07 Mar 2014 14:45:21 GMT
ETag: "53037f98-cc2f46"
Server: nginx/1.4.4
Connection: Keep-Alive
Content-Type: video/mp4
Accept-Ranges: bytes
Last-Modified: Tue, 18 Feb 2014 15:43:20 GMT
Content-Length: 13381446