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

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

问题描述

我正在为iPad设计一个Web应用程序,它在移动游戏中使用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代理请求,我得到以下aprox 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.

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


  1. 首先它请求2个字节,这样就可以读取'Content-Range'标题。

  2. 现在请求全部内容(?)

  3. -

  4. 继续执行步骤1。& 2.再次(??)

  5. -

  6. 它现在只请求部分文件(???)

  1. First it requests 2 bytes which allows it to read the 'Content-Range' header.
  2. Now it request the entire content (?)
  3. -
  4. It proceeds to do step 1. & 2. again (??)
  5. -
  6. It now requests only parts of the file (???)

如果文件较大,最后的步骤将会更多。我已经测试了多达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-full-length-mp3

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

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