【问题标题】:Read HttpWebResponse with Content-Type : multipart/x-mixed-replace in c#使用 Content-Type 读取 HttpWebResponse:c# 中的 multipart/x-mixed-replace
【发布时间】:2020-01-09 08:38:47
【问题描述】:

我有一个需要显示视频源的 Asp.Net 应用程序。视频提要有一个 Httpwebresponse,内容类型为“multipart/x-mixed-replace;边界=--myboundary'。

    Stream responseStream = response.GetResponseStream()
    StreamReader streamReader = new StreamReader(streamToRead,Encoding.ASCII)
    return streamReader.ReadToEnd();

我尝试过使用字符串阅读器,但没有用。 请让我知道如何使用 content-type multipart/x-mixed-replace 读取(访问)响应;边界=--myboundary .

提前致谢。

问候, 普拉文

【问题讨论】:

    标签: asp.net httpwebrequest httpwebresponse


    【解决方案1】:

    我假设您的意思是您的 ASP.Net 应用程序是一个服务器,它对 UWP 或 WPF 等 .NET Core 客户端进行多部分响应,因为该服务器就像一个无法显示的控制台应用程序视频。否则,服务器可以按原样将流块逐块转发给客户端。您不能 .ReadToEnd() 获取尚未结束的实时视频源。

    // using Microsoft.Net.Http.Headers; // to parse the boundary from the content type
    // using Microsoft.AspNetCore.WebUtilities; // to read each part of multipart
    
    using (var responseStream = response.GetResponseStream())
    {
        var contentType = MediaTypeHeaderValue.Parse(response.ContentType);
        var boundary = HeaderUtilities.RemoveQuotes(contentType.Boundary).Value;
        for (var streamReader = new MultipartReader(boundary, responseStream); ;)
        {
            var nextFrame = await streamReader.ReadNextSectionAsync(cancellationToken);
            if (nextFrame == null)
                break;
            DisplayOneFrameCallback(nextFrame.Body);
        }
    }
    

    然后您需要实现DisplayOneFrameCallback(),它一次接收一帧的流并提供如下位图解码器:

    // for UWP, copy the stream to an InMemoryRandomAccessStream via a Dispatcher
    myBitmapImage.SetSourceAsync(memoryStreamOfOneFrame);
    

    如果没有回答,请提供您的问题的更多详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-29
      • 2017-03-04
      • 2011-07-05
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多