0

I'm trying to download video from any site for that i need video url stream but in webresponse i get -1 stream all the time i don't understan..... plzz help me out

WebRequest request = WebRequest.Create(selecturl);
            string l=request.ToString();
            MessageBox.Show(l + " webrequest  ");
            WebResponse response = request.GetResponse();
            MessageBox.Show( request.Method = "Get");
            Stream stream = response.GetResponseStream();
           // StreamReader streamReader = new StreamReader(stream);

           //Download in chunks
          int k = stream.ReadByte();
        MessageBox.Show(k + " ");

            byte[] buffer = new byte[1024];
        //get Total size

            MessageBox.Show(response.ContentLength.ToString());
Rahul Bh
  • 123
  • 2
  • 15
  • From the [docs for HttpWebResponse.ContentLength](https://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.contentlength(v=vs.110).aspx): "If the Content-Length header is not set in the response, ContentLength is set to the value -1." – Jon Skeet Sep 30 '17 at 19:35
  • @JonSkeet i set the header but it still show the same length...... WebHeaderCollection headerCollection = response.Headers; MessageBox.Show(headerCollection.ToString() + "header message box "); it show the header but the content length is same – Rahul Bh Oct 01 '17 at 06:31
  • How are *you* setting the header? It's in the response, which you don't control - only the server does. Unless this is a server you control as well, which you haven't said... – Jon Skeet Oct 01 '17 at 06:32
  • so what i have to do set header ? – Rahul Bh Oct 01 '17 at 07:28
  • Where? In the client code? You don't. The point is that the server is meant to set it *if* it wants to... but it doesn't have to, and it may not know how much data there'll be anyway. (Particularly if it's streaming video in an adaptive bandwidth way, adjusting the quality based on how quickly you're downloading.) – Jon Skeet Oct 01 '17 at 07:37
  • so what am i do now to get the content length of the url ?? any suggestion... – Rahul Bh Oct 01 '17 at 08:10
  • You don't. If the server doesn't tell you how much content there is, you won't know until you read it. Imagine the server starts returning data and will keep going until some condition is met... what length could it possibly give at the start of the response? – Jon Skeet Oct 01 '17 at 08:14
  • I believe this might solve your problem https://stackoverflow.com/questions/5596747/download-stream-file-from-url-asp-net – Muhammad Saqib Oct 02 '17 at 08:27

0 Answers0