Response.Flush() throws System.Web.HttpException

后端 未结 4 988
迷失自我
迷失自我 2021-02-05 21:31

I have a HttpHandler that I\'m using to handle certain images on a client\'s website. When I\'m outputting the image stream to the response object and call Flush occasionally an

相关标签:
4条回答
  • 2021-02-05 22:09

    I realise this is an old post but it came up when I was looking for an answer to a similar issue. The following is largely verbatim from this SO answer. More background info is available at Is Response.End() considered harmful?.

    Replace this: HttpContext.Current.Response.End();

    With this:

    HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
    HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
    
    0 讨论(0)
  • 2021-02-05 22:12

    Personally in your implementation since the next line is Response.End(), just remove the call to Response.Flush() as Response.End() takes care of everything for you.

    0 讨论(0)
  • 2021-02-05 22:17

    While I agree with Mitchel - there's little need to call flush as you're about to call End, if you're using this elsewhere, you could try calling Response.IsClientConnnected first.

    Gets a value indicating whether the client is still connected to the server.

    0 讨论(0)
  • 2021-02-05 22:31

    For future readers..

    I ran into this case where Response.End() throws an error because the client is disconnected.

    An error occurred while communicating with the remote host. The error code is 0x80070057

    Oddly a CRLF in the StatusDescription was causing the connection to close.

    Response.StatusDescription = ex.Message;
    

    Cannot insert the value NULL into column '', table ''; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated

    Removing it fixed my problem.

    Response.StatusDescription = ex.Message.Replace("\r\n", " ");
    
    0 讨论(0)
提交回复
热议问题