Tunnel Web API 通过 MVC 动作响应视频流
Tunnel Web API video stream response through MVC action
http://www.codeproject.com/Articles/820146/HTTP-Partial-Content-In-ASP-NET-Web-API-Video
使用上面的 link,我创建了一个网络 api 调用,如果我直接调用网络 api,它将 return 视频和播放没有问题。在生产环境中,网络 api 调用将在防火墙后面,public 无法直接访问。由于太长的原因,我无法向面向 public 的网站添加 Web api 服务。
我想通过 MVC 操作和 return 将 Web api 控制器的确切结果传递给用户。 Web api return 是一个 HttpResponseMessage,所以我使用了下面的代码,认为我可以通过隧道传递响应,但它似乎根本不起作用。
public HttpResponseMessage Play(string fileName)
{
using (var client = new HttpClient())
{
var userName = Impersonation.Instance.CurrentImpersonatedUser;
var url = string.Format("{0}/api/Player/Play?f={1}",
this.pluginSettings["VirtualVideoTrainingServiceURL"],
fileName);
var result = client.GetAsync(url).Result;
return result;
}
}
当我调用 MVC 操作时,我只是在浏览器中得到它。
Result
我认为它以某种方式序列化数据,但我无法证明或反驳该理论。我是否需要解析来自 Web 服务的响应,然后将其转换为文件结果?如有任何帮助,我们将不胜感激!
WebAPI 处理程序将响应组成 headers、内容和其他参数,并将根据 Http 规范和与服务 client/consumer 的内容协商发送给用户。
另一方面,您可以只从服务获取 JSON 内容并通过 MVC 操作传递它。
您可以使用object<HttpResponseMessage>.Content
获取HttpResponseMessage
中返回的内容
MVC 使用 IActionResult
(由 System.Web.Mvc.Controller
使用)来组合执行的操作方法的结果,WebAPI 的 System.Web.Http.ApiController
使用 IHttpActionResult
来组合输出。
好的,我找到了答案。
http://www.dotnetcurry.com/aspnet-mvc/998/play-videos-aspnet-mvc-custom-action-filter
警告:下面的代码需要清理。
以此为例,我创建了一个名为 VideoResult 的 ActionResult,看起来像
private byte[] Buffer = null;
private string FileName = string.Empty;
private ContentRangeHeaderValue Range = null;
private string Length = string.Empty;
public VideoResult(byte[] buffer, string fileName, ContentRangeHeaderValue range, string length)
{
this.Buffer = buffer;
this.FileName = fileName;
this.Range = range;
this.Length = length;
}
/// <summary>
/// The below method will respond with the Video file
/// </summary>
/// <param name="context"></param>
public override void ExecuteResult(ControllerContext context)
{
//The header information
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.PartialContent;
if (this.Range != null)
{
context.HttpContext.Response.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", this.Range.From, this.Range.To, this.Range.Length));
}
context.HttpContext.Response.AddHeader("Content-Type", "video/mp4");
context.HttpContext.Response.AddHeader("Content-Length", this.Length);
context.HttpContext.Response.BinaryWrite(Buffer);
}
我从内容中检索字节数组作为来自 StreamContent(或 PushStreamContent)的 ByteArray,并将该数据传递到上面的 VideoResult。
var sc = ((StreamContent)result.Content).ReadAsByteArrayAsync();
return new VideoResult(sc.Result, fileName, result.Content.Headers.ContentRange,
result.Content.Headers.ContentLength.ToString());
这也允许用户搜索视频。我希望直接传递来自 Web 服务的结果,但如上所示,响应差异太大,因此需要转换为 MVC 操作结果。
http://www.codeproject.com/Articles/820146/HTTP-Partial-Content-In-ASP-NET-Web-API-Video
使用上面的 link,我创建了一个网络 api 调用,如果我直接调用网络 api,它将 return 视频和播放没有问题。在生产环境中,网络 api 调用将在防火墙后面,public 无法直接访问。由于太长的原因,我无法向面向 public 的网站添加 Web api 服务。
我想通过 MVC 操作和 return 将 Web api 控制器的确切结果传递给用户。 Web api return 是一个 HttpResponseMessage,所以我使用了下面的代码,认为我可以通过隧道传递响应,但它似乎根本不起作用。
public HttpResponseMessage Play(string fileName)
{
using (var client = new HttpClient())
{
var userName = Impersonation.Instance.CurrentImpersonatedUser;
var url = string.Format("{0}/api/Player/Play?f={1}",
this.pluginSettings["VirtualVideoTrainingServiceURL"],
fileName);
var result = client.GetAsync(url).Result;
return result;
}
}
当我调用 MVC 操作时,我只是在浏览器中得到它。 Result 我认为它以某种方式序列化数据,但我无法证明或反驳该理论。我是否需要解析来自 Web 服务的响应,然后将其转换为文件结果?如有任何帮助,我们将不胜感激!
WebAPI 处理程序将响应组成 headers、内容和其他参数,并将根据 Http 规范和与服务 client/consumer 的内容协商发送给用户。
另一方面,您可以只从服务获取 JSON 内容并通过 MVC 操作传递它。
您可以使用object<HttpResponseMessage>.Content
HttpResponseMessage
中返回的内容
MVC 使用 IActionResult
(由 System.Web.Mvc.Controller
使用)来组合执行的操作方法的结果,WebAPI 的 System.Web.Http.ApiController
使用 IHttpActionResult
来组合输出。
好的,我找到了答案。
http://www.dotnetcurry.com/aspnet-mvc/998/play-videos-aspnet-mvc-custom-action-filter
警告:下面的代码需要清理。
以此为例,我创建了一个名为 VideoResult 的 ActionResult,看起来像
private byte[] Buffer = null;
private string FileName = string.Empty;
private ContentRangeHeaderValue Range = null;
private string Length = string.Empty;
public VideoResult(byte[] buffer, string fileName, ContentRangeHeaderValue range, string length)
{
this.Buffer = buffer;
this.FileName = fileName;
this.Range = range;
this.Length = length;
}
/// <summary>
/// The below method will respond with the Video file
/// </summary>
/// <param name="context"></param>
public override void ExecuteResult(ControllerContext context)
{
//The header information
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.PartialContent;
if (this.Range != null)
{
context.HttpContext.Response.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", this.Range.From, this.Range.To, this.Range.Length));
}
context.HttpContext.Response.AddHeader("Content-Type", "video/mp4");
context.HttpContext.Response.AddHeader("Content-Length", this.Length);
context.HttpContext.Response.BinaryWrite(Buffer);
}
我从内容中检索字节数组作为来自 StreamContent(或 PushStreamContent)的 ByteArray,并将该数据传递到上面的 VideoResult。
var sc = ((StreamContent)result.Content).ReadAsByteArrayAsync();
return new VideoResult(sc.Result, fileName, result.Content.Headers.ContentRange,
result.Content.Headers.ContentLength.ToString());
这也允许用户搜索视频。我希望直接传递来自 Web 服务的结果,但如上所示,响应差异太大,因此需要转换为 MVC 操作结果。