Response.Close() vs Response.End()

When using with AJAX is it better to use close or end?  (or both (and what order)).

Close:  Closes the socket connection to a client.  
End:  Sends all currently buffered output to the client, stops execution of the page, and raises the EndRequest event.  

I know this might sound like a simple question but i think it might be the cause of a bug in my program.

LVL 12
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

try this:


also, i doubt you actually need to be using those methods in your app....if you think there is a related bug, try not using them at all and check results.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
For normal process you can use:

For esceptional case, say you are catching some exception and want to display error message in client, you can use only     context.Response.End(); after context.Response.Write().
UnexplainedWaysAuthor Commented:
The problem i think is that i've now made an AJAX call on <body onunload>.  The javascript get's called and the server page is contacted, however it never returns back to the javascript (thats ok, just telling the server the page closed).

My guess is that you can get away without using Response.Close() &Response.End() in a normal ajax call, however after 2 call's to the server i can't make a third.  It seems to crash everything.  I guess it has a limit of 2 connections and they wern't closed or ended.
UnexplainedWaysAuthor Commented:
All sorted now,

When its a normal AJAX call, it needs to have .End(), and doesnt require .Close().  However, when its calling when the page is being closed, it requires the .Close() and it can't have the .End().

Simple if/else sorts that one out.  Thanks for the help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.