【发布时间】:2018-02-20 15:37:41
【问题描述】:
我正在使用 .Net Core 2.0 开发我的 ASP.Net API,我想请求外部公共流并使用我自己的 GET 路由转发它。
外部流是 MJPEG 内容类型。
由于我使用的是最新版本的 .NET Core,PushStreamContent 不再可用。
这是负责连接和流处理的类:
internal class LiveViewStream
{
HttpClient _client = new HttpClient();
WebClient webClient = new WebClient();
public Stream outputStream = new MemoryStream();
public Stream GetVideoTCP()
{
string url = "http://87.139.76.248:8081/cgi-bin/faststream.jpg";
return webClient.OpenRead(url);
}
public void Main()
{
var bytesRead = 0;
var buffer = new byte[65536];
using (Stream stream = GetVideoTCP())
{
do
{
bytesRead = stream.Read(buffer, 0, buffer.Length);
outputStream.Write(buffer, 0, bytesRead);
}
while (stream.Position != stream.Length);
}
}
这是 GET 路线:
// GET: api/<controller>
[HttpGet]
public HttpResponseMessage Get()
{
var video = new LiveViewStream();
video.Main();
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(video.outputStream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("video/x-motion-jpeg");
return response;
}
这段代码给了我while 方法不被支持的错误。我尝试了不同的方法,但它们都不起作用,我要么在发出 GET 请求时得到一个文件,要么什么都没有。
谁能帮我解决这个问题?
【问题讨论】:
标签: c# asp.net asp.net-web-api .net-core