获取Response.output大小? [英] Get Response.output size?
问题描述
我的代码现在是这样的:
My code is now 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());
错误是
NotSupportedException was unhandled by user code
推荐答案
Response.OutputStream
不支持 Length
属性;你无法得到长度.一个问题是,如果启用了 Keep-Alive
(通常情况下),则 Stream
可能会超出 Response
.这就是为什么您根本无法获得 Length
的原因...但是...
The Response.OutputStream
does not support a Length
property; you can not get the length. One problem is, that Stream
could outlive the Response
if, as is usually the case, Keep-Alive
is enabled. That's why you can't get a Length
at all... But...
还要注意,这里的Stream不仅发送内容,还发送标头.这使得使用您希望的方式完全不合适(因为您生成的 Content-Length
标头将是错误的.)因此,即使您可以获得 Length
,那不是你想要的.
Also note that the Stream here is sending more than just the content - it's also sending the headers. That makes it wholly inappropriate to use how you are hoping to use it (since your resulting Content-Length
header would be incorrect.) So even if you could get the Length
, it would not be what you want.
无论如何,这又是不可能的:您要设置的标头是一部分,该 Stream
上将会发生什么....看到问题了吗?
And it's impossible again, anyway: The header you are attempting to set is part of what would be going out on that Stream
.... see the problem?
不过,FosterZ为您提供了一条尝试解决此问题的不同途径.您需要获得真正要测量的长度-而不是测量流的长度.
FosterZ has given you a different route to go to try to solve this problem, though; you need to get the length of what you really want to measure - not of the stream.
这篇关于获取Response.output大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!