我试图将范围功能集成到我的HttpServer中,但我一直在收到此错误:
类型' System.Net.HttpListenerException'未处理的异常 发生在System.dll
中附加信息:I / O操作因中止而中止 线程退出或应用程序请求
我想提一下我的HttpListener是在Thread上运行的,我使用的是BeginGetContext。
有什么想法导致这个错误吗?
if (File.Exists(RequestedFile))
{
Stream ReadStream = new FileStream(RequestedFile, FileMode.Open, FileAccess.Read);
//Get Range
try
{
string[] RangeArray = HttpRequest.Headers.GetValues("Range")[0].Replace("bytes=", "").Split('-');
long.TryParse(RangeArray[0], out StartChunk);
if (RangeArray[1].Trim().Length > 0)
{
long.TryParse(RangeArray[1], out EndChunk);
}
if (EndChunk == 0) { EndChunk = (int)ReadStream.Length; }
ChunkRange = EndChunk - StartChunk;
}
catch
{
StartChunk = 0;
EndChunk = ReadStream.Length;
ChunkRange = EndChunk - StartChunk;
}
HttpResponse.ContentType = "video/mp4";
HttpResponse.ContentLength64 = ChunkRange;
//Start File Processing
byte[] Buffer = new byte[ChunkRange];
ReadStream.Position = StartChunk;
ReadStream.Read(Buffer, 0, (int)ChunkRange);
ReadStream.Flush();
ReadStream.Close();
//Flush to Response
HttpResponse.OutputStream.Write(Buffer, 0, Buffer.Length);//error occurs here
HttpResponse.StatusCode = (int)HttpStatusCode.OK;
HttpResponse.OutputStream.Flush();
}
else { HttpResponse.StatusCode = (int)HttpStatusCode.NotFound; }
HttpResponse.OutputStream.Close();
更新 这似乎是由于Chrome发生的。 Read accepted answer here