Copying from one stream to another?

前端 未结 4 2099
伪装坚强ぢ
伪装坚强ぢ 2021-02-09 03:07

For work, the specification on my project is to use .Net 2.0 so I don\'t get the handy CopyTo function brought about later on.

I need to copy the response s

4条回答
  •  执笔经年
    2021-02-09 04:03

    You can get rid of the BufferedStream, it's only useful if you are reading small chunks of the stream. Just get the response stream in a variable and use that:

    Stream response = HttpResponse.GetResponseStream();
    

    A buffer that is too small will reduce the performance. You can use a larger buffer, so that at least the data from an entire IP packet fits. I looked around a bit, and 4096 bytes should be enough for that. You can really use any size up to 85 kb, after that it's allocated in the large objects heap, which you should avoid to do when there is no reason for it.

    Other than that, it's about as efficient as it gets.

提交回复
热议问题