c# - Setting the http status midway through a streamed response -


i streaming http response , erroring midway through execution, have simulated below. end user seeing 200 response because understand status sent in response header. time error occurs, , have caught , reset status 500 late. how can set "real" status 500 after have streamed original header?

var httpresponse = httpcontext.current.response; httpresponse.bufferoutput = false; httpresponse.contenttype = "application/xml"; try {     httpresponse.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + environment.newline);     httpresponse.write($"<{constants.xmlheadertag}>");     var theline = 0;     while (some condition)     {         if (theline++ == 3) throw new exception("got error");         var innerxml = "some xml";         httpresponse.binarywrite(encoding.utf8.getbytes(innerxml));     }     httpresponse.write($"</{constants.xmlheadertag}>");     httpresponse.outputstream.flush();     httpresponse.end(); } catch (exception ex) {     httpresponse.status = 500;     logerror(ex); } 

you cannot. http header is before data in payload. once headers sent: sent. cannot set header after this, , if could, no client understand meant, since there's no way receive header during payload. can kill response; client know haven't got (assuming sent content-length header), , xml malformed (not correctly terminated), they'll figure out broken sooner or later.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -