C#:HttpListener服务内容错误 [英] C#: HttpListener Error Serving Content

查看:218
本文介绍了C#:HttpListener服务内容错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实施了类似于这个
的东西真正的区别是

I have implemented something similar to this only real difference is

string filename = context.Request.RawUrl.Replace("/", "\\").Remove(0,1);
string path = Uri.UnescapeDataString(Path.Combine(_baseFolder, filename));

,以便我可以遍历子目录。这对于网页和其他文本文件类型非常有用,但是当尝试提供媒体内容时,我会收到例外

so that I can traverse to subdirectories. This works great for webpages and other text file types but when trying to serve up media content I get the exception


HttpListenerException:I / O
操作已被中止,因为
线程退出或应用程序
请求

HttpListenerException: The I/O operation has been aborted because of either a thread exit or an application request


InvalidOperationException:无法关闭流,直到写入所有字节。

InvalidOperationException: Cannot close stream until all bytes are written.

在使用声明中。

有关如何处理此事或停止这些异常的任何建议?

Any suggestions on how to handle this or stop these exceptions?

谢谢

推荐答案

我应该提到我在浏览器中使用Google Chrome(Google Chrome似乎没有关心MIME类型,当它看到音频时会尝试使用它,就像在HTML5播放器中一样),但是如果您尝试在页面中托管媒体内容,这也是适用的。

I should mention that I am using Google Chrome for my browser (Google Chrome doesn't seem to care about the MIME types, when it sees audio it will try to use it like it's in a HTML5 player), but this is also applicable if you are trying to host media content in a page.

无论如何,我正在检查我的h带有提示的电子邮件,并注意到Chrome将3个请求传递到服务器。我开始玩其他浏览器,并注意到他们没有这样做,但根据浏览器和我的硬编码为MIME类型,我会得到一个疯狂的文本页面,或下载的文件。

Anyways, I was inspecting my headers with fiddler and noticed that Chrome passes 3 requests to the server. I started playing with other browsers and noticed they did not do this, but depending on the browser and what I had hard coded as the MIME type I would either get a page of crazy text, or a download of the file.

在进一步检查时,我注意到chrome将首先请求该文件。然后再次使用几个不同的标头请求文件,最显着的是范围标题。第一个字节= 0,然后下一个大小取决于文件的大小(超过3个请求可以取决于文件的大小)。

On further inspection I noticed that chrome would first request the file. Then request the file again with a few different headers most notably the range header. The first one with byte=0- then the next with a different size depending on how large the file was (more than 3 requests can be made depending how large the file is).

所以有问题。 Chrome将首先要求该文件。一旦看到类型,它会发送另一个请求,我似乎在寻找文件的大小(byte = 0-),然后另一个请求文件的下半部分或类似的东西,允许一种流式传输时使用HTML5。我快速编码了一些东西来处理MIME类型,并将HTML5页面与音频组件一起投入,并发现其他浏览器也执行此操作(IE除外)

So there was the problem. Chrome will first ask for the file. Once seeing the type it would send another request which seems to me looking for how large the file is (byte=0-) then another one asking for the second half of the file or something similar to allow for a sort of streaming experienced when using HTML5. I coded something quickly up to handle MIME types and threw a HTML5 page together with the audio component and found that other browsers also do this (except IE)

所以这里是一个快速解决方案,我不再收到这些错误

So here is a quick solution and I no longer get these errors

string range = context.Request.Headers["Range"];
int rangeBegin = 0;
int rangeEnd = msg.Length;
if (range != null)
{
    string[] byteRange = range.Replace("bytes=", "").Split('-');
    Int32.TryParse(byteRange[0], out rangeBegin);
    if (byteRange.Length > 1 && !string.IsNullOrEmpty(byteRange[1]))
    {
       Int32.TryParse(byteRange[1], out rangeEnd);
    }
}

context.Response.ContentLength64 = rangeEnd - rangeBegin;
using (Stream s = context.Response.OutputStream)
{
    s.Write(msg, rangeBegin, rangeEnd - rangeBegin);
}

这篇关于C#:HttpListener服务内容错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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