1

我遇到了一些问题,找不到任何解决方案,我有以下代码:

    private void GetFileFromURL(string url,NetworkCredential cred)
    {
        try
        {
            using (var client = new WebClient())
            {
                if (cred != null)
                {
                    //client.Credentials = cred;
                }
                using (var stream = client.OpenRead(url))
                {
                    int count = 0;
                    do
                    {
                        if (Response.IsClientConnected)
                        {
                            //byte[] buf = new byte[500024];
                            byte[] buf = new byte[500024];
                            count = stream.Read(buf, 0, 500024);
                            StreamToClient(buf, count);
                        }
                        else
                            break;

                    } while (stream.CanRead && count > 0);
                    Response.End();
                }
            }
        }
        catch { }
    }
    bool isHeaderSent = false;
    private void StreamToClient(byte[] bytes,int length)
    {
        if (!isHeaderSent)
        {
            Response.Clear();
            Response.Buffer = false;

            Response.ContentType = "video/mp4";

            isHeaderSent = true;
        }
        Response.OutputStream.Write(bytes, 0, length);
    }

此代码位于 getVideo.aspx.cs 和我用于流式传输我的 mp4 文件的代码,它在 http 上运行良好,但是当我将其更改为 https(在 iis 上)时,我在客户端上什么也得不到,在客户端我使用这 :

axWindowsMediaPlayer1.URL = 视频源;

其中 videoSource 类似于: https:// localhost /getVideo.aspx

从我在谷歌上阅读的内容来看,它可能与标题有关,在使用 https 时添加特殊标题,但不确定它是否相关。

泰。

4

1 回答 1

1

Okis,我为其他有同样问题的人写这个:

Https的响应时间窗口较短,因此,您必须告诉客户端您的数据有多长,这样客户端就不会认为流结束了,您真正需要添加的是这些标头:

        Response.AddHeader("content-disposition", "filename=video.mp4");

        Response.AddHeader("Content-Length", totalBytes.ToString());

其中 totalBytes 是保存文件中字节数的长整数,您需要在将文件发送到客户端之前从服务器获取它。

于 2013-10-10T14:20:55.147 回答