读取webreponse.GetResponseStream(),慢慢来。

 

在做毕业设计时,在IIS于winform程序之间传输图片,

                HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);

                HttpWebResponse webreponse = (HttpWebResponse)webrequest.GetResponse();
                Stream stream = webreponse.GetResponseStream();

以为利用Stream进行读取时,Stream.Read可以一次将所有的字节内容都读取到,想不到不行,

                 byte[] photocontent = new Byte[webreponse.ContentLength];

                 stream.Read(photocontent, 0, (int)webreponse.ContentLength );只能读取到3799字节的数据,在网上查了好久,没有找到怎么回事,后来,想到了解决大文件上传显示进度问题时,用到的HttpWorkerRequest.ReadEntityBod(),需要检测是否读取完毕,进行多次读取才可以。

                int readecount = 0;
                while (readecount < (int)webreponse.ContentLength)
                {
                    readecount += stream.Read(photocontent, readecount, (int)webreponse.ContentLength - readecount);

                }

就行了。

     想了想,不知道问什么,毕竟

                HttpWebResponse webreponse = (HttpWebResponse)webrequest.GetResponse();

的时候就已经把整个Response获取了。呵呵


posted @ 2008-08-31 16:56  孤独流星  阅读(9932)  评论(1编辑  收藏  举报