Get size Response.output?

My code now looks like this:

var xmlSerializer = new XmlSerializer(typeof(DataSet)); xmlSerializer.Serialize(Response.Output, ds); 

After that I want to show the size, but this gives me an error:

 Response.AddHeader("Content-Length", Response.OutputStream.Length.ToString()); 

Error

 NotSupportedException was unhandled by user code 
-5
source share
2 answers

Response.OutputStream does not support the Length property; you cannot get the length. One of the problems is that Stream can survive Response if, as it usually happens, Keep-Alive enabled. That is why you cannot get Length at all ... But ...

Also note that Stream here sends more than just content - it also sends headers. This makes it completely inappropriate to use the way you hope to use it (since your resulting Content-Length header would be wrong.) Therefore, even if you can get the Length , this is not what you want.

And this is impossible again, anyway: the header you are trying to set is part of what will happen on this Stream .... see the problem?

FosterZ gave you another route to try to solve this problem; you need to get the length of what you really want to measure, not the flow.

+2
source

I did this before how long my code was as shown below, you might get some kind of idea or hint:

 string output = encoding.GetString(memoryStream.ToArray()); memoryStream = new MemoryStream(output.Length); byte[] buffer = encoding.GetBytes(output); //buffer.Length(); is this the length you want ? 
+1
source

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


All Articles