🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.web
Article #451

Re: downloading with wininet

#451
From: Remy Lebeau (TeamB)
Date: Tue, 02 Jan 2007 18:44
95 lines
4041 bytes
"Boba" <Boba@somewhere.world> wrote in message
news:459a89d6@newsgroups.borland.com...

> ...hmmm... both gave the same result. i wish i knew why.

Considering that none of your code is doing any error handling, you
did not specify the contents of the values you are passing, and you
did not specify on which line the failure is actually occuring, there
is no way to diagnose your problem.  You need to provide more
information.

> here's a sample call: DownloadFile("download.microsoft.com",
>
"/download/6/a/f/6af940c1-2774-4ebb-8546-2c83068af231/Windows2000-KB92
6247-x86-E
> NU.EXE");

Ok so far.  Though again, there is no reason to separate the server
from the path like that.  You should be passing it as a single
complete URL string, ie:


DownloadFile("http://download.microsoft.com/download/6/a/f/6af940c1-27
74-4ebb-8546-2c83068af231/Windows2000-KB926247-x86-ENU.EXE");

> > I suggest you use HttpQueryInfo() to retreive the size of the data
> > before then entering the loop.
>
> InternetQueryDataAvailable() is supposed to do that.

Your looping code is wrong anyway.  You are not using the value that
InternetQueryDataAvailable() returns.  You must read everything that
InternetQueryDataAvailable() reports, or else it cannot report more
data later.  This is clearly stated in the documentation.  You need to
change your loop to something more like the following instead:

    BYTE Buffer[1024];
    DWORD dwDataSize, dwBytesToRead, dwBytesRead, dwBytesDownloaded;

    dwBytesDownloaded = 0;

    while( InternetQueryDataAvailable(hHttpRequest, &dwDataSize, 0,
0) )
    {
        if( dwDataSize == 0 )
            break;

        do
        {
            dwNumToRead = min(dwDataSize, sizeof(Buffer));

            if( !InternetReadFile(hHttpRequest, Buffer, dwBytesToRead,
&dwBytesRead) )
                break;

            // write the data...
            dwDataSize -= dwBytesRead;
            dwBytesDownloaded += dwBytesRead;
        }
        while( dwDataSize != 0 );
     }


However, I strongly suggest you use HttpQueryInfo() instead of
InternetQueryDataAvailable().  Then you need only 1 loop instead of 2,
since the download already knows the full size of the data ahead of
time, ie:

    BYTE Buffer[1024];
    DWORD dwDataSize, dwBytesToRead, dwBytesRead, dwBytesDownloaded,
dwIndex = 0;
    DWORD dwLen = sizeof(dwDataSize);

    dwBytesDownloaded = 0;

    if( HttpQueryInfo(hHttpRequest, HTTP_QUERY_CONTENT_LENGTH |
HTTP_QUERY_FLAG_NUMBER, &dwDataSize, &dwLen, &dwIndex) )
    {
        while( dwDataSize != 0 )
        {
            dwNumToRead = min(dwDataSize, sizeof(Buffer));

            if( !InternetReadFile(hHttpRequest, Buffer, dwBytesToRead,
&dwBytesRead) )
                break;

            // write the data...
            dwDataSize -= dwBytesRead;
            dwBytesDownloaded += dwBytesRead;
        }
    }


Gambit
Technical Details
Message-ID: <459aac49$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!news.glorb.com!newsgate.cistron.nl!transit.news.xs4all.nl!xs4all!news2.euro.net!213.132.189.2.MISMATCH!multikabel.net!feed20.multikabel.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4599f9c5@newsgroups.borland.com> <459a2c13$1@newsgroups.borland.com> <459a89d6@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: feeec,66a46201c905b896
X-Google-Attributes: gidfeeec,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.web
References: <4599f9c5@newsgroups.borland.com> <459a2c13$1@newsgroups.borland.com> <459a89d6@newsgroups.borland.com>
Subject: Re: downloading with wininet
Date: Tue, 2 Jan 2007 10:44:17 -0800
Lines: 97
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 75.83.34.171
Message-ID: <459aac49$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1167764553 75.83.34.171 (2 Jan 2007 11:02:33 -0800)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!news.glorb.com!newsgate.cistron.nl!transit.news.xs4all.nl!xs4all!news2.euro.net!213.132.189.2.MISMATCH!multikabel.net!feed20.multikabel.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail