C#: HttpListener Error Serving Content

2019-02-19 07:18发布

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: The I/O operation has been aborted because of either a thread exit or an application request

Followed by

InvalidOperationException: Cannot close stream until all bytes are written.

In the using statement.

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

Thanks

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-02-19 07:36

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.

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.

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).

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);
}
查看更多
三岁会撩人
3楼-- · 2019-02-19 07:51

Try:

using (Stream s = context.Response.OutputStream)
{
      s.Write(msg, 0, msg.Length);
      s.Flush()             
}
查看更多
登录 后发表回答