wcf stream 不知道长度的情况下,读取stream

时间:2022-05-02 14:39:07

http://bbs.csdn.net/topics/360163784

            string filepath = @"http://ww4.sinaimg.cn/thumbnail/6741e029jw1dfzrlqr07kj.jpg";
Stream stream = WebRequest.Create(filepath).GetResponse().GetResponseStream(); int size = ;
int read = ;
MemoryStream ms = new MemoryStream();
byte[] buffer = new byte[size];
do
{
buffer = new byte[size];
read = stream.Read(buffer, , size);
ms.Write(buffer, , read);
} while (read > ); Console.WriteLine(ms.Length.ToString());

或者:

        private void CopyStream(Stream instream, Stream outstream)
{
const int bufferLen = ;
byte[] buffer = new byte[bufferLen];
int count = ;
while ((count = instream.Read(buffer, , bufferLen)) > )
{
outstream.Write(buffer, , count);
}
}

--