在网上有一个 my.swf 文件,有2M左右,System.Net.WebClient client = new System.Net.WebClient();
 client.DownloadFile("http://www.xxxx.com/my.swf", "d:\\tmp\\my.swf");用以上方法,下载后的文件只有 9K,这是为什么?
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.xxxx.com/my.swf"); 
            HttpWebResponse   response   =(HttpWebResponse)request.GetResponse(); 
            Stream   s   =   response.GetResponseStream(); 
            int   lenth=(int)response.ContentLength; 
            byte[]   buf=new   byte[lenth]; 
            s.Read(buf,0,lenth);
            FileStream file = new FileStream("d:\\tmp\\my.swf", FileMode.Create);
            file.Write(buf, 0, buf.Length);
            s.Close();
用以上方法,下载后的文件也只有9K.这个问题该如何解决呢?

解决方案 »

  1.   

           
    要保证头信息没有错误 public void DownFile(string url, string Filename)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";            request.Timeout = 300000;
                request.KeepAlive = true;
                request.AllowAutoRedirect = true;            request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; CIBA)";            request.ContentType = "application/x-www-form-urlencoded";            HttpWebResponse webresponse = null;
                try
                {
                    webresponse = request.GetResponse() as HttpWebResponse;
                    if (webresponse != null)
                    {
                        Stream st = webresponse.GetResponseStream();
                        Stream so = new FileStream(Filename, FileMode.Create);
                        byte[] by = new byte[1024];
                        int osize = st.Read(by, 0, (int)by.Length);
                        while (osize > 0)
                        {
                            so.Write(by, 0, osize);
                            osize = st.Read(by, 0, (int)by.Length);
                        }
                        so.Close();
                        st.Close();
                    }
                }
                catch
                {
                }
                finally
                {
                    if (webresponse != null)
                    {
                        webresponse.Close();
                        webresponse = null;
                    }
                    if (request != null)
                    {
                        request.Abort();
                        request = null;
                    }
                }
            }
      

  2.   

                                    client.DownloadFile(OriUri,   DownFileName);                                Stream   str   =   client.OpenRead(OriUri);                                //StreamReader   reader   =   new   StreamReader(str);
                                    byte[]   mbyte   =   new   byte[100000];
                                    int   allmybyte   =   (int)mbyte.Length;
                                    int   startmbyte   =   0;
     
                                    while   (allmybyte   >   0)
                                    {
                                            int   m   =   str.Read(mbyte,   startmbyte,   allmybyte);
                                            if   (m   ==   0)
                                                    break;
                                            startmbyte   +=   m;
                                            allmybyte   -=   m;
                                    }                                FileStream   fstr   =   new   FileStream(DownFilePath,   FileMode.OpenOrCreate,   FileAccess.Write);
                                    fstr.Write(mbyte,   0,   startmbyte);
                                    str.Close();
                                    fstr.Close(); 
      

  3.   

    不会是只把播放器下载下来,内容没下载吧。
    现在一般的flash都有个播放器,然后缓冲数据的。