In C# HttpResponse, when does the response stream starts to be sent to the client?

55 Views Asked by At

I am using streams in my http response and was wondering when the stream starts to be sent to the client that sent the request. I thought it was sent when the response was returned but one of my colleague was arguing that it would be better to start sending it when the body stream is updated (when i do the CopyToAsync in my code below) but then, he was wondering how it was possible to set the headers afterward. If anyone has deep knowlodge about the way stream are handled with http reponse, I am pretty curious to know the answer and I couldn't find reliable info online.

Here is the code that started the debate.

HttpResponseData response = httpRequest.CreateResponse(HttpStatusCode.OK);
using (FileStream file = new FileStream(outputPDF, FileMode.Open, FileAccess.Read))
{
    await file.CopyToAsync(response.Body);
}

response.Headers.Add("Content-Type", "application/pdf");
return response;
0

There are 0 best solutions below