HttpHandler response never returns

We have a rather sophisticated httphandler for image processing. It basically transmits any part of the image of any size that is requested. Some clients use this handler without any problems. But we have one place that gives us problems, and now it also creates problems in my development environment.

What happens is that the client never receives anything on certain requests. So query 1 and 2 are fine, but query 3 and 4 never ends.

  • During debugging, I see that the server is ready and completed the request.
  • The client is still waiting for the result (debugging with fiddler2 indicates that no response has been received)

The code we use to stream the image,

        if (!context.Response.IsClientConnected)
        {
            imageStream.Close();
            imageStream.Dispose();
            return;
        }

        context.Response.BufferOutput = true;
        context.Response.ContentType = "image/" + imageformat;

        context.Response.AppendHeader("Content-Length", imageStream.Length.ToString());

        if (imageStream != null && imageStream.Length > 0 && context.Response.IsClientConnected)
            context.Response.BinaryWrite(imageStream.ToArray());

        if (context.Response.IsClientConnected)
            context.Response.Flush();

        imageStream.Close();
        imageStream.Dispose();

ImageStream is a MemoryStream with image content.

response.Flush() .

GC.Collect() , , , . , , ?

IIS 5 (Win XP), IIS 6 (Win 2003), .NET framework v2.

+3
4

, . , , ( ), , , .

HttpWebResponse , , GetResponseStream, .

. , . HTTP. Content-Length ASP.NET .

flush, ASP.NET , . . , . Content-Length, .

, , Content-Length, , , . Marc - , , MemoryStream WriteTo .

+2

-, , (.. MemoryStream).

:

const int BUFFER_SIZE = 4096; // pick your poison
bute[] buffer = new byte[BUFFER_SIZE];
int bytesRead;

while((bytesRead = inStream.Read(buffer, 0, BUFFER_SIZE)) > 0)
{
    outStream.Write(buffer, 0, bytesRead);
}

(.Response.BufferOutput = false).

, , / (.Response.Close()).

+4

. BufferOutput true? , , , false.

: Wireshark, , . Fiddler HTTP, .

+2

WebRequests . . WebResponses .

using (HttpWebResponse test_resp = (HttpWebResponse)test_req.GetResponse())
{
}

, ..

0

Source: https://habr.com/ru/post/1699402/


All Articles