Click here to Skip to main content
15,305,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to use WebClient to get a movie size, without download it?

Without downloading the actual file, I can not use the "DownloadProgressChanged" method, which is linked by the actual download process.
I only wish to interrogate the page and extract only the data, without the actual content.
Thanks.

What I have tried:

C#
WebClient wc;
void Download4()
{
    wc = new WebClient();

    Uri uri = new Uri(url);
    wc.DownloadFileAsync(uri, movieName);
    wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
    wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
}

 void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
 {
     label1.Text = Website.SizeSuffix(e.BytesReceived) + "/" + Website.SizeSuffix(e.TotalBytesToReceive);


         TotalBytesToReceive = TotalBytesToReceive + e.TotalBytesToReceive;

 }

 void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
 {
    label2.Text = TotalBytesToReceive.ToString();
 }
Posted
Updated 26-Feb-16 19:15pm
v2
Comments
DotNetSteve 26-Feb-16 22:18pm
   
Does the site you are going to list the file by name and filesize? If so look for this rather than trying to download.
_Q12_ 27-Feb-16 0:37am
   
I can see the filesize (partial and total) while downloading the file.
I can not see any property/method that is passively do it for me.
   
If you want to ask how to achieve something, better don't tell how. Isn't that logical? "Use WebClient" is a wrong part of this question.
—SA

1 solution

Of course you don't need to download a whole file, because HTTP tells you how big the whole stream will be. But to solve the problem this way, you should not use really rudimentary System.Net.WebClient. You have to use HttpWebRequest instead:
WebClient Class (System.Net)[^],
HttpWebRequest Class (System.Net)[^].

Here is what I can suggest: please download the full code of my HttpDownloaded I provided in my past answer: how to download a file from internet[^].

This downloader allows to continue downloading of incompletely downloaded file, which apparently is based on getting the full size of a network stream from the very beginning. You will see how to do it. This is a very small piece of code, only one file; quite easy to read and understand.

See also my past answers:
how to download a file from the server in Asp.net 2.0[^],
FTP: Download Files[^],
How to get the data from another site[^].

—SA
   
Comments
_Q12_ 27-Feb-16 2:23am
   
Thank you for your response, mister Sergey.
yah, definitely I will put that code into work.
I like how you comment the end of the closing bracket - ocien interesnaia :)
I didnt explain that this code that I posted is supposed to work into a winapp and NOT a webapp(like asp.net). I think in asp I can escape very easy with this problem. I dare to say, your method is good for my winapp, right? I will test it anyway.
Sergey Alexandrovich Kryukov 27-Feb-16 10:56am
   
You are welcome.
It does not matter with what application type to work in this case, the technique is good for any .NET code. But in other cases, yes, you should better tag your application type, and often the UI framework/library, etc.

Anyway, will you accept the answer formally?

—SA
_Q12_ 27-Feb-16 13:08pm
   
Thank you again for your response mister Sergey.
I will accept your answer, but first I must understand your code.
///
I sincerely don't get the code related to web access. The rest of the code I do understand. But not the one for the web. You clearly know a lot about Internet protocols than I do. I have to exercise with this HttpWebRequest/HttpWebResponse a bit to be able to use it alone.
Right now, I took parts from your code and try to adapt it to mine, and I am looking stupid at your code, because I don't know where to implement it.... I am very frustrated on myself.
...
the engine of your code is this little segment:
long preloadedLength = GetExistingFileLength(fname);
if file exist> if (preloadedLength > 0)

//here is the actual extraction of information(in this case the Length)
And looking into the webRequest methods/properties, this method that you used here is the ONLY ONE that can be used? Oh boy, I am so angry by switching from (starting)c++ to c#.
webRequest.AddRange((int)preloadedLength);

//and this "proxi" thing is wow - is an intermediary for requests from clients seeking resources from other servers.... and is good for.... for me too?...I have no idea what is that for. I must read about it.
webRequest.Proxy = proxy; //SA??? or DefineProxy
//and now you request a response? is having no sense.
webResponse = (HttpWebResponse)webRequest.GetResponse();
   
Sorry, I don't understand what do you mean by not getting the code "related to Web access". It is remotely related to Web, it's more about HTTP. I cannot see what is the problem understanding it. What "having no sense"? Thus is the major part of the use of HttpWebRequest, if you want to receive data from the Web. You send a request, the server sends you back a response. Perhaps you first need to read what HTTP does, or you already understand it, I'm not sure.
I tried to minimize everything unrelated to the main problem, including the proxy. Yes, read about it if you are interested in it. Proxy is a server which should behave as an origin server for the clients, but actually it relays packages...
What else..? You don't have preloaded length, so don't need to worry about it. Focus on the number you get from response, webResponse.ContentLength...
"Only one" or not is really unrelated to the problem. I just explain the solution for you; no need to look around. You wanted an advice on how to do it, or what?...
—SA
_Q12_ 28-Feb-16 2:25am
   
Thank you, mister Sergey...
I must have more experience with http methods.
Sergey Alexandrovich Kryukov 29-Feb-16 11:08am
   
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900