This is a mirror of official site: http://jasper-net.blogspot.com/

Why not to use HttpResponse.Close and HttpResponse.End

| Tuesday, October 5, 2010
I’ve seen many times developers use HttpResponse.Close and HttpResponse.End when they want to end the request and send a response to the client. If you read the MSDN explanation about HttpResponse.Close Method, in Remarks you will see the following explanation:

This method terminates the connection to the client in an abrupt manner and is not intended for normal HTTP request processing. The method sends a reset packet to the client, which can cause response data that is buffered on the server, the client, or somewhere in between to be dropped.

You might use this method in response to an attack by a malicious HTTP client. However, typically you should call CompleteRequest() instead if you want to jump ahead to the EndRequest event and send a response to the client.

So, as it says, this method terminates the connection to the client in an abrupt manner and is not intended for normal HTTP request processing.

On the other hand, the HttpResponse.End Method, from the first versions of .NET Framework, was provided for compatibility with the COM based Web Programming technology that was predecessor of the ASP.NET (the classic ASP), which in this case is not anymore needed to be used.

So, if you read the MSDN remarks for both HttpResponse.Close and HttpResponse.End methods, you will see that in normal cases, we should replace both with the HttpApplication.CompleteRequest method. This method will directly call the EndRequest event and the request will end.

See the comparison with the following code examples.

Here is one code example where I’ve used Response.End to terminate the current connection to the client.

protected void Page_Load(object sender, EventArgs e)
{
   Response.Write("Hello Hajan");
   Response.End();            
   Response.Write("<br />Goodbye Hajan");
}

After calling the Response.End method, no other code line after this will execute. So, if you have any other code that needs to be executed, this will skip executing the code, which might result in a wrong or unexpected behavior.

Read more: Hajan's Blog

Posted via email from .NET Info

0 comments: