2

In a web site I capture the http POST request with a HttpModule as in this answer.

After reading the body of the POST in the context's BeginRequest event I would like to return as quickly as possible an "OK" response to the client.
What would be the best way to prevent further processing in IIS (7.5 integrated mode)?

Community
  • 1
  • 1
Gerard
  • 13,023
  • 14
  • 72
  • 125

2 Answers2

3

I believe this one is the fastest:

Response.Clear();
Response.ClearHeaders();

Response.StatusCode = 200;
Response.StatusDescription = "OK";

Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
volpav
  • 5,090
  • 19
  • 27
  • 1
    `CompleteRequest` will halt the request. The Gerard wants to actually send an OK response. – Samuel Neff Jan 23 '11 at 02:14
  • 1
    "CompleteRequest" is implicitly called by "Response.End". But in a latter case you'll most likely get a ThreadAbordException (see the following KB article: http://support.microsoft.com/kb/312629). I've added a Response.Flush (although I don't think that Gerard wanted to send something except the status code). – volpav Jan 23 '11 at 16:07
  • .End() gives a ThreadAbordException while .CompleteRequest() doesn't. On the other hand .Clear(), .ClearHeaders(), .Flush() do not seem to add anything. Response.Write("OK") gives an OK response in the body also when using CompleteRequest(). – Gerard Jan 23 '11 at 19:26
1

Call HttpResponse.End(). It will flush any pending content (your OK response) and then end the request.

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182