c#通过http流式传输到iphone

我试图通过http流式传输video到iphone没有带.net的流媒体服务器。 经过一些测试,我发现如果你只是将iphone兼容video上传到你的服务器,iis7工作正常,iphone在小缓冲时间后开始播放video,并继续在后台下载。

我的问题是,我无法用.net做到这一点。 我试过了

public static void SmallFile(string filename, string filepath, string contentType) { try { FileStream MyFileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read); long FileSize; FileSize = MyFileStream.Length; byte[] Buffer = new byte[(int)FileSize]; MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length); MyFileStream.Close(); HttpContext.Current.Response.ContentType = contentType; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)); HttpContext.Current.Response.BinaryWrite(Buffer); } catch { HttpContext.Current.Response.ContentType = "text/html"; HttpContext.Current.Response.Write("Downloading Error! " + filename + " not found!"); } HttpContext.Current.Response.End(); } 

或者

 public static void ResumableFile(string filename, string fullpath, string contentType) { try { FileStream myFile = new FileStream(fullpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); BinaryReader br = new BinaryReader(myFile); try { HttpContext.Current.Response.AddHeader("Accept-Ranges", "bytes"); HttpContext.Current.Response.Buffer = false; long fileLength = myFile.Length; long startBytes = 0; //int pack = 10240; //10K bytes int pack = 1048576; //1024K bytes if (HttpContext.Current.Request.Headers["Range"] != null) { HttpContext.Current.Response.StatusCode = 206; string[] range = HttpContext.Current.Request.Headers["Range"].Split(new char[] { '=', '-' }); startBytes = Convert.ToInt64(range[1]); } HttpContext.Current.Response.AddHeader("Content-Length", (fileLength - startBytes).ToString()); if (startBytes != 0) { HttpContext.Current.Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength)); } HttpContext.Current.Response.AddHeader("Connection", "Keep-Alive"); HttpContext.Current.Response.ContentType = contentType; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)); br.BaseStream.Seek(startBytes, SeekOrigin.Begin); int maxCount = (int)Math.Floor((double)((fileLength - startBytes) / pack)) + 1; for (int i = 0; i < maxCount; i++) { if (HttpContext.Current.Response.IsClientConnected) { HttpContext.Current.Response.BinaryWrite(br.ReadBytes(pack)); } else { i = maxCount; } } } catch { HttpContext.Current.Response.ContentType = "text/html"; HttpContext.Current.Response.Write("Downloading Error! " + filename + " not found!"); } finally { br.Close(); myFile.Close(); } } catch { HttpContext.Current.Response.ContentType = "text/html"; HttpContext.Current.Response.Write("Downloading Error!" + filename + " not found!"); } } 

在两者中,我收到错误,说服务器配置不正确。 然后我删除了

  HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)); 

部分因此它不会触发完全下载但结果是相同的。

我已经检查了服务器的响应,当我直接下载文件时,没有来自服务器的不同/额外标头。

我试图找到什么将使iphone缓冲并开始播放video,当我直接下载video文件,我怎么能用.net实现它

如果你为什么我不使用iis,我需要根据带宽限制进行某种水蛭保护

有人有任何经验吗?

UPDATE

这是iis的第一个请求和回复

 GET /test.mp4 HTTP/1.1 Host: 192.168.2.200 User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; tr-tr) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16 Cache-Control: max-age=0 Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: tr-tr Accept-Encoding: gzip, deflate Connection: keep-alive HTTP/1.1 200 OK Content-Type: video/mp4 Last-Modified: Wed, 23 Dec 2009 20:12:57 GMT Accept-Ranges: bytes ETag: "e6ad9151c84ca1:0" Server: Microsoft-IIS/7.5 X-Powered-By: ASP.NET Date: Wed, 23 Dec 2009 21:07:19 GMT Content-Length: 2301438 

第二个请求和响应

 GET /test.mp4 HTTP/1.1 Host: 192.168.2.200 Range: bytes=0-2269183 Connection: close User-Agent: Apple iPhone OS v3.1.2 CoreMedia v1.0.0.7D11 Accept: */* Accept-Encoding: identity HTTP/1.1 206 Partial Content Content-Type: video/mp4 Last-Modified: Wed, 23 Dec 2009 20:12:57 GMT Accept-Ranges: bytes ETag: "e6ad9151c84ca1:0" Server: Microsoft-IIS/7.5 X-Powered-By: ASP.NET Date: Wed, 23 Dec 2009 21:11:33 GMT Connection: close Content-Length: 2269184 Content-Range: bytes 0-2269183/2301438 

据我所知,iphone在第二次请求时请求不同的字节,依此类推。 无论如何得到c#将这些字节范围发送到客户端?

我想我已经在http://dotnetslackers.com/articles/aspnet/Range-Specific-Requests-in-ASP-NET.aspx找到了我的解决方案

我会进行某种消息嗅探。

基本上,我设置了一些可以将video流式传输到iPhone(如TVersity),然后设置一些内容来拦截TVersity和iPhone之间的流量,以便您可以检查HTTP请求和响应。

这是一篇关于如何设置TVersity的博客文章:

http://geeks.pirillo.com/profiles/blogs/2300301:BlogPost:38497

相对url“/ iPhone”是您想要嗅探流量的url。 它应该阐明正确的消息交换模式,以便将video下载到iPhone。

试试这个http://code.google.com/p/talifun-web/wiki/StaticFileHandler

或者我的第一个回复链接有源代码