我通过 stream = response.GetResponseStream();获取了一个stream对象,
可是对象属性为下
CanRead =false;
CanSeek=false;
CanWrite=false;要怎么样才能把这个流写成文件了?

解决方案 »

  1.   

    可否用别的方式保存?是不是只是要保存html为文件?
    webform还是mvc?
      

  2.   

    网络流,比如doc,txt格式文件
      

  3.   


                Stream stream = null;//加入这个就是取得的stream
                byte[] bs = new byte[stream.Length];
                stream.Read(bs,0,bs.Length);
                FileStream fstream = new FileStream("c:\\1.dat", FileMode.Create);
                fstream.Write(bs, 0, bs.Length);
                fstream.Close();
      

  4.   

    在创建一个文件流
    so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
    so.Write();//把stream  流写入
      

  5.   

     stream = response.GetResponseStream();
    stream .Write();方法
      

  6.   

    谢谢4楼的,不过现在获取是stream属性如下 CanRead false bool
    CanSeek false bool
    CanTimeout true bool
    CanWrite false bool
    + Length 'stream.Length' threw an exception of type 'System.NotSupportedException' long {System.NotSupportedException}
    + Position 'stream.Position' threw an exception of type 'System.NotSupportedException' long {System.NotSupportedException}
    不能读,不能写。
      

  7.   


    不能读,不能写的流能这样?
    CanRead =false;
    CanSeek=false;
    CanWrite=false
      

  8.   

    本帖最后由 bdmh 于 2012-03-23 10:51:51 编辑
      

  9.   

     public static Stream getRedirectResponseStream(String uri)
            {
                  
                Stream stream = null;
                try
                {                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
                    req.AllowAutoRedirect = false;
                    req.Method = "GET";
                    req.KeepAlive = false;
                    using (HttpWebResponse wr = (HttpWebResponse)req.GetResponse())
                    {//在这里对接收到的页面内容进行处理                     stream = getResponseRedirect(wr);
                        
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                    return null;
                }
                return stream;
            }        public static Stream getResponseRedirect(HttpWebResponse wr)
            {
                Stream stream = null;
                try
                {                String realuri = wr.Headers["Location"];
                    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(new Uri(realuri));
                    CookieContainer cc = new CookieContainer();
                    req.KeepAlive = false;                String cookie = wr.Headers["Set-Cookie"];
                    String[] arr = cookie.Split(';');                cc.SetCookies(new Uri(realuri), arr[0]);
                    req.CookieContainer = cc;
                    req.Method = "GET";                using (WebResponse response = req.GetResponse())
                    {//在这里对接收到的页面内容进行处理  
                        stream = response.GetResponseStream();
                    }            }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                    return null;
                }
                return stream;
            }
      

  10.   

    因为没有你的环境,可以试试帮助中的例子StreamReader readStream = new StreamReader( ReceiveStream, encode );
    ReceiveStream为接收的流,如果能你再去处理readStream 
      

  11.   

    http://blog.csdn.net/yes16ws/article/details/7329769 符合你的要求
      

  12.   

    不要用using                using (WebResponse response = req.GetResponse())
                    {//在这里对接收到的页面内容进行处理  
                        stream = response.GetResponseStream();
                        //StreamReader s = new StreamReader(stream);
                    }
    上面那个改一下
                    WebResponse response = req.GetResponse();
                    stream = response.GetResponseStream();
      

  13.   


    呵呵,这个我也刚刚发现,修改了stream对象就可读了。对C# 语法不太了解,谢谢指点。
      

  14.   

    这里发言,表示您接受了CSDN社区的用户行为准则。