移动 Safari 发出多个视频请求 [英] Mobile Safari makes multiple video requests

查看:42
本文介绍了移动 Safari 发出多个视频请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 iPad 设计一个 Web 应用程序,它在移动 safari 中使用 HTML5.我通过运行 .NET Framework v2.0 的 IIS 7 上托管的 ASP.NET .ashx 文件手动传输文件.

I am designing a web application for iPad which makes use of HTML5 in mobile safari. I am transmitting the file manually through an ASP.NET .ashx file hosted on IIS 7 running .NET Framework v2.0.

基本代码部分如下所示:

The essential code looks partly like this:

// If we receive range header only transmit partial file
if (context.Request.Headers["Range"] != null)
{
    var fi = new FileInfo(filePath);
    long fileSize = fi.Length;

    // Read start/end index
    string headerRange = context.Request.Headers["Range"].Replace("bytes=", "");
    string[] range = headerRange.Split('-');
    int startIndex = Convert.ToInt32(range[0]);
    int endIndex = Convert.ToInt32(range[1]);

    // Add header Content-Range,Last-Modified
    context.Response.StatusCode = (int)HttpStatusCode.PartialContent;
    context.Response.AddHeader(HttpWorkerRequest.GetKnownResponseHeaderName(HttpWorkerRequest.HeaderContentRange), String.Format("bytes {0}-{1}/{2}", startIndex, endIndex, fileSize));
    context.Response.AddHeader(HttpWorkerRequest.GetKnownResponseHeaderName(HttpWorkerRequest.HeaderLastModified), String.Format("{0:r}", fi.CreationTime));

    long length = (endIndex - startIndex) + 1;
    context.Response.TransmitFile(filePath, startIndex, length);
}
else
    context.Response.TransmitFile(filePath);

现在让我困惑不已的是请求 safari 似乎使用的协议.通过 fiddler 代理请求,我得到了大约 2MB 文件的以下内容.

Now what confuses me to no end is the the protocols for requesting that safari seems to use. From proxying the requests through fiddler i get the following for an aprox 2MB file.

注意:当请求 mp4 文件时,直接通过 IIS 7 提供服务,协议和请求量是相同的

  1. 首先它请求 2 个字节,以便读取Content-Range"标头.
  2. 现在它请求全部内容 (?)
  3. -
  4. 它继续执行第 1 步. &2. 再次(??)
  5. -
  6. 它现在只请求文件的一部分 (???)

如果文件更大,最后的步骤会更多.我测试了多达 99 个请求,其中每个请求都包含文件的一部分,均等分.这是有道理的,也是我认为的预期.我无法理解的是为什么它在最终请求不同部分的文件之前对前 2 个字节发出 2 个初始请求以及对整个文件发出 2 个请求.

If the file is larger the last steps will be many more. I have tested up to 99 request where each request contains a part of the file equally split. This makes sense and is what would be expected I think. What I cannot make sense of is why it makes 2 initial request for the first 2 bytes as well as 2 requests for the entire file before it finally requests the file in different parts.

根据我的结论,这会导致文件被下载 2 到 3 次,具体取决于文件的长度以及用户观看的时间是否足够长.

As I conclude this results in the file being downloaded between 2 - 3 times, depending on the length of the file and whether the user watches it long enough.

任何人都可以理解这种行为并解释我可以做些什么来防止多次下载.谢谢.

Can anybody make sense of this behavior and maybe explain what I can do to prevent multiple downloads. Thanks.

推荐答案

根据我对您问题的评论,我过去也遇到过类似的问题.如果您可以控制服务器(我没有),您可以尝试的一件事是禁用 gzip 或文件的身份编码.我相信在对整个内容的第一个请求(列表中的#2)中,它要求使用 gzip 编码(压缩)的内容.也许您可以将 IIS 配置为不为 gzip 编码请求提供文件.

Per my comment to your question, I've had a similar issue in the past. One thing you could try if you have control of the server (I did not) is to disable either gzip or identity encoding of the file. I believe that in the first request for the entire content (#2 in your list) it asks for the content with gzip encoding (compressed). Perhaps you can configure your IIS to not to serve the file for a gzip-encoding request.

这是我关于该主题的原始(未回答)问题:

Here is my original (unanswered) question on the subject:

https://stackoverflow.com/questions/4855485/mpmovieplayercontroller-not-playing-全长-mp3

这篇关于移动 Safari 发出多个视频请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆