Solved

Determining the length of a file with WinInet

Posted on 2002-04-24
7
660 Views
Last Modified: 2010-04-04
Hi,

I am using this code with the asynchronous WinInet to determine the length of a file...

dwBufferLen:=SizeOf(pr.dwTotalLength);
dwIndex:=0;
if (HttpQueryInfo(pr.hOpenURL, HTTP_QUERY_CONTENT_LENGTH or HTTP_QUERY_FLAG_NUMBER, @pr.dwTotalLength, dwBufferLen, dwIndex) = FALSE) then pr.dwTotalLength:=0;

This is done so that I can display a progress bar whilst the file is downloading.  For many sites this works fine, however for some sites I find that it always returns zero.  For example...

www.google.com
www.yahoo.com
www.ask.com

As a result I can't display a progress bar for some sites when the download takes place because I am unable to determine the length of the file before the download starts, so it isn't possible to know how much progress has been made.

I guess it doesn't work with some sites because the length information isn't stored in the HTTP header??  Do you know of any way around this problem?

Thanks in Advance,

Richard
0
Comment
Question by:Richard2000
7 Comments
 
LVL 1

Expert Comment

by:Alone
Comment Utility
But server does support downloading resume?

WBR...
0
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility

Richard,

It is possible that the servers on these sites do not generate the "Content-Length:" header item. This is not a required header, though most sites do return it. If I remember correctly, this is the header used to return the results for the HTTP_QUERY_CONTENT_LENGTH flag. Other than that, I'm not sure why it would fail.
Have you checked GetLastError() in the cases where it does fail? This may help give you/us a little more info to go on.

Russell
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 50 total points
Comment Utility

Richard,

Did some more research into this (MSDN and other sources). In cases where you are not able to get the resource size, there is not much your going to be able to do in regards to displaying upload/download progress. One possible work around to this is to modify your progress to be timed based, as well as progress based. For the resources where you are not able to get size, you could then update the meter based on timed increments, giving the user the appearance of progress. Hope this helps.

Russell


0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
monitoring..
haha .. didn't use 'listening' :)
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
Can you inspect the TCP/IP packet information?
When transfering files, the individual packets are numbered and (I think) include the total number of packets being sent.

Will any other transfer protocol, such as FTP, provide size or transfer progress data?
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
interested ;P
0
 

Author Comment

by:Richard2000
Comment Utility
Hi Russell and all,

Thank you for your comments and for looking into this for me.  I thought that the problem was caused by the length information not being present because it works fine on most sites, but fails on some sites.

So this is what I've chosen to do.  For sites where the length information isn't present, I estimate the length and use that for the progress bar.  Since it is mainly web pages that I am downloading, I use 20K as the length and make that go up to 80% on the progress bar and no further.  Then when the download has finally completed I set this to 100%.  Of course, this isn't 100% accurate but it is better than having a blank progress bar and it is hardly noticable.  It is also easy to code.

Richard
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now