C#流通过HTTP iphone [英] c# streaming over http to iphone

查看:130
本文介绍了C#流通过HTTP iphone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图视频流通过HTTP iphone没有与.NET中的streming服务器。
一些测试后,我发现,如果你只是上传iPhone兼容的视频到你的服务器,IIS7工作得很好,iphone startsplaying视频后,小缓冲时间,并继续在后台下载。



我的问题是,我无法用.NET来做到这一点。我曾尝试与

 公共静态无效SMALLFILE(字符串的文件名,字符串文件路径,字符串的contentType)
{
尝试
{
的FileStream MyFileStream =新的FileStream(文件路径,FileMode.Open,FileAccess.Read,FileShare.Read);
长的文件大小;
=文件大小MyFileStream.Length;
字节[]缓冲区=新的字节[(INT)文件大小]
MyFileStream.Read(缓冲液,0,(int)的MyFileStream.Length);
MyFileStream.Close();
HttpContext.Current.Response.ContentType的contentType =;
HttpContext.Current.Response.AddHeader(内容处置,附件;文件名=+ HttpUtility.UrlEncode(文件名,System.Text.Encoding.UTF8));
HttpContext.Current.Response.BinaryWrite(缓冲);

}

{
HttpContext.Current.Response.ContentType =text / html的;
HttpContext.Current.Response.Write(!下载错误+文件名+没有找到!);
}
HttpContext.Current.Response.End();
}

 公共静态无效ResumableFile(字符串的文件名,字符串完整路径,字符串的contentType)
{

{
的FileStream MYFILE =新的FileStream(完整路径,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
BinaryReader BR =新BinaryReader(MYFILE);

{
HttpContext.Current.Response.AddHeader(接受范围,字节);
HttpContext.Current.Response.Buffer = FALSE;
长文件长度= myFile.Length;
长startBytes = 0;

// INT包= 10240; // 10K字节
INT包= 1048576; // 1024K字节
如果(HttpContext.Current.Request.Headers [范围]!= NULL)
{
HttpContext.Current.Response.StatusCode = 206;
的String []范围= HttpContext.Current.Request.Headers [范围]斯普利特(新的char [] {'=',' - '});
startBytes = Convert.ToInt64(范围[1]);
}
HttpContext.Current.Response.AddHeader(内容长度,(文件长度 - startBytes)的ToString());
如果(startBytes!= 0)
{
HttpContext.Current.Response.AddHeader(内容范围的String.Format(字节{0} - {1} / {2 },startBytes,文件长度 - 1,文件长度));
}
HttpContext.Current.Response.AddHeader(连接,保持活动);
HttpContext.Current.Response.ContentType的contentType =;
HttpContext.Current.Response.AddHeader(内容处置,附件;文件名=+ HttpUtility.UrlEncode(文件名,System.Text.Encoding.UTF8));

br.BaseStream.Seek(startBytes,SeekOrigin.Begin);
INT MAXCOUNT =(int)的Math.Floor((双)((文件长度 - startBytes)/包))+ 1;

的for(int i = 0; I< MAXCOUNT;我++)
{
如果(HttpContext.Current.Response.IsClientConnected)
{
HttpContext.Current.Response.BinaryWrite(br.ReadBytes(包));
}
,否则
{
I = MAXCOUNT;
}
}
}

{
HttpContext.Current.Response.ContentType =text / html的;
HttpContext.Current.Response.Write(!下载错误+文件名+没有找到!);
}
终于
{
br.Close();
myFile.Close();
}
}

{
HttpContext.Current.Response.ContentType =text / html的;
HttpContext.Current.Response.Write(!下载错误+文件名+没有找到!);
}
}

在这两个,我得到错误说服务器未配置正常。然后我删除

  HttpContext.Current.Response.AddHeader(内容处置,附件;文件名=+ HttpUtility。以UrlEncode(文件名,System.Text.Encoding.UTF8)); 



部分,因此它不会触发全下载,但结果是一样的。



我已经检查从服务器响应,并没有任何的不同/额外的标头从服务器来的时候我直接下载文件。



我是什么试图findout什么会使得iPhone缓冲,并开始播放视频的时候我直接下载视频文件,我怎么能使用.NET implemnet它



如果youwonder为什么我就是不'T使用IIS,我需要把某种水蛭protecttion由于带宽限制



任何人有任何的先进经验?



更新



这是第一个请求,并从IIS



<$ p响应$ p> GET /test.mp4 HTTP / 1.1
主机:192.168.2.200
的User-Agent:Mozilla的/ 5.0(iPhone; U; CPU的iPhone OS 3_1_2,例如Mac OS X ; TR-TR)为AppleWebKit / 528.18(KHTML,例如Gecko)版本/ 4.0移动/ 7D11 Safari浏览器/ 528.16
缓存控制:最大年龄= 0
接受:应用/ XML,是application / xhtml + XML,为text / html; q = 0.9,text / plain的; q = 0.8,图像/ PNG,* / *; q = 0.5
接受语言:TR-TR
接受编码:gzip,紧缩
连接:保持活动


HTTP / 1.1 200 OK
内容类型:视频/ MP4
的Last-Modified:星期三12月23日2009年20点12分57秒GMT
接受-范围:字节
的ETag:e6ad9151c84ca1:0
服务器:Microsoft-IIS / 7.5
的X技术,通过:ASP。 NET
日期:星期三,2009年12月23日21时07分19秒GMT
的Content-Length:2301438

并第二次请求和响应

  GET /test.mp4 HTTP / 1.1 
主机:192.168.2.200
范围:字节= 0-2269183
连接:关闭
的User-Agent:苹果iPhone OS V3.1.2 CoreMedia v1.0.0.7D11
接受:* / *
的Accept-Encoding:身份



HTTP / 1.1 206部分内容
内容类型:视频/ MP4
的Last-Modified:星期三,2009年12月23日二十时12分57秒GMT
接受-范围:字节
的ETag:e6ad9151c84ca1:0
服务器:Microsoft-IIS / 7.5
的X技术,通过:ASP.NET
日期:星期三2009年12月23日21时11分33秒GMT
连接:关闭
的Content-Length:2269184
内容 - 范围:字节0-2269183 / 2301438

据我了解iphone请求对第二个要求等不同的字节。
反正是有让C#发送这些字节范围的客户端?


解决方案

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


i am trying to stream video over http to iphone without a streming server with .net. After some tests i found that if you just upload iphone compatible video to your server, iis7 works just fine and iphone startsplaying video after small buffer time and continues to download in the background.

My problem is, i am unable to do it with .net. I have tried with

    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();
}

or with

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!");
    }
}

In both, i get error saying server is not configured properly. I then removed

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

part so it won't trigger full download but result was the same.

I have checked responses from server and there is no different/extra header coming from server when i directly download file.

What i am trying to findout what would be enabling iphone to buffer and start playing video when i directly download video file and how can i implemnet it with .net

If youwonder why i just don't use iis, i need to put some sort of leech protecttion due to bandwidth restrictions

Anyone have any exprience?

UPDATE

Here is the first request and response from 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

And second request and response

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

As i understand iphone requests different bytes on second request and so on. Is there anyway getting c# to send those bytes range to the client?

解决方案

I think i've found my solution at http://dotnetslackers.com/articles/aspnet/Range-Specific-Requests-in-ASP-NET.aspx

这篇关于C#流通过HTTP iphone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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