http ContentLength 0 Download Issues

 As shown by http download something, WebResponse response = request.GetResponse (); response debugging Figure

ContentLength 0 Content-Range video but is value results downloaded byte 0

 

The revised Code:

 

 public static int GetTotalSize(string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.CookieContainer = BaseService.CookiesContainer;
            WebResponse response = request.GetResponse();
            int len = Int32.Parse(response.Headers["Content-Range"].ToString().Split(new char[] { '/' })[1]);
            return len;
        }

        public  static  byte [] SendRangeGetRequest ( String URL) 
        { 
            the try 
            { 
                int len = GetTotalSize (URL); 
                the HttpWebRequest Request = (the HttpWebRequest) the WebRequest.Create (URL); 
                request.method = " GET " ; 
                request.AddRange ( " bytes " , 0 , len);   // this is the key to add it, response of ContentLength have a value

                 IF (CookiesContainer == null ) 
                { 
                    CookiesContainer =new CookieContainer();
                }

                request.CookieContainer = CookiesContainer;  //启用cookie

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream response_stream = response.GetResponseStream();

                int count = (int)response.ContentLength;
                int offset = 0;
                byte[] buf = new byte[count];
                while (count > 0)  // read return data 
                {
                     int n-= response_stream.Read (buf, offset, COUNT);
                     IF (n-== 0 ) BREAK ; 
                    COUNT - = n-; 
                    offset + = n-; 
                } 
                return buf; 
            } 
            the catch 
            { 
                return  null ; 
            } 
        }

 

Finally File.WriteAllBytes (savePath, bytes); you can download to the local

Guess you like

Origin www.cnblogs.com/lovewuhan/p/10974072.html