为什么不要使用Response.Close()

 

   不要使用Response.Close()

   不要使用Response.Close()

   不要使用Response.Close()

   

   1  因为:Close()方法会调用HttpWorkerRequest.CloseConnection()方法。终止(Terminate)与客户端的套接字连接,并使得服务器,客户端以及之间设施上的缓存(buffer)失效。导致发送到客户端的数据丢失。

   

   2 方法Response.End()是为了兼容经典ASP程序,在Asp.NET 1.0中引入的,在调用后会抛出ThreadAbortException异常。成功时则中止(abort)当前的线程,处理管道触发EndRequest事件,不再处理之后的代码。会以同步的方式将响应内容发送(flush)给客户端。

      由于.NET 设计原因,Response.End()在WebForm框架下可以终止代码执行,不再处理End()之后的代码。在MVC框架下则只是返回响应流,不会中止代码执行。

     

   3 此外还可以使用HttpApplication.CompleteRequest()方法结束请求。

      Response.Flush();

      this.Context.ApplicationInstance.CompleteRequest();

   

   综上所述:只有代码发生错误(恶意的攻击),希望终止对于客户端的响应/连接时才可以使用Response.Close()

   如果你想结束请求,并向客户端发送请求则应该优先使用HttpApplication.CompleteRequest()方法。

参考文章:

1 http://weblogs.asp.net/hajan/why-not-to-use-httpresponse-close-and-httpresponse-end

2 http://stackoverflow.com/questions/1886729/httpresponse-end-vs-httpresponse-close-vs-httpresponse-suppresscontent

3 http://www.cnblogs.com/v5wa/p/3165367.html

posted @ 2015-12-04 16:51  pracZHEN  阅读(5248)  评论(0编辑  收藏  举报