http - C#: HttpListener Error Serving Content -
i have implemented similar this real difference
string filename = context.request.rawurl.replace("/", "\\").remove(0,1); string path = uri.unescapedatastring(path.combine(_basefolder, filename));
so can traverse subdirectories. works great webpages , other text file types when trying serve media content exception
httplistenerexception: i/o operation has been aborted because of either thread exit or application request
followed
invalidoperationexception: cannot close stream until bytes written.
in using statement.
any suggestions on how handle or stop these exceptions?
thanks
i should mention using google chrome browser (google chrome doesn't seem care mime types, when sees audio try use it's in html5 player), applicable if trying host media content in page.
anyways, inspecting headers fiddler , noticed chrome passes 3 requests server. started playing other browsers , noticed did not this, depending on browser , had hard coded mime type either page of crazy text, or download of file.
on further inspection noticed chrome first request file. request file again few different headers notably range header. first 1 byte=0- next different size depending on how large file (more 3 requests can made depending how large file is).
so there problem. chrome first ask file. once seeing type send request seems me looking how large file (byte=0-) 1 asking second half of file or similar allow sort of streaming experienced when using html5. coded handle mime types , threw html5 page audio component , found other browsers (except ie)
so here quick solution , no longer 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); }
Comments
Post a Comment