🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Downloading and resuming

#733
From: Will Bright
Date: Mon, 06 Nov 2006 17:22
86 lines
3392 bytes
I am using the following code to download a file from and http
server, there are some server that work well; the entire file
comes down.  And there are other servers that seem to time out,
I am not sure if they do time out or not, but the downloads are
incomplete.

I am trying to resume the download so it can continue on, but my
code does not seem to be doing just that.  I may have a bunch or
errors in the code, if anyone is familiar with it can they point
out what I need to do to fix it.

	HINTERNET hSession = NULL;
	HINTERNET hFile = NULL;
	DWORD dwContentLen;
	DWORD dwBufLen = sizeof(dwContentLen);
	DWORD dwRead;

	TSaveDialog* SaveDialog = NULL;
	TFileStream* FileStream = NULL;

	try
	{
		hSession = InternetOpen("Windows Internet Explorer",
					INTERNET_OPEN_TYPE_PRECONFIG, NULL,	NULL, 0);
		if(!hSession)
			throw(Exception("unable to to create internet session"));

		hFile = InternetOpenUrl(hSession, edtURL->Text.c_str(), NULL,
								0, INTERNET_FLAG_RELOAD, NULL);
		if(!hFile)
			throw(Exception(AnsiString("unable to create a file handle to the specified url") + edtURL->Text));

		if(!HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
						 (LPVOID)&dwContentLen,	&dwBufLen, 0))
			throw(Exception(AnsiString("Failed to obtain the length of the file")));

		SaveDialog = new TSaveDialog(NULL);
		DWORD dwBytesRead = NULL;
		if(SaveDialog->Execute())
		{
			if(FileExists(SaveDialog->FileName))
			{
				FileStream = new TFileStream(SaveDialog->FileName, fmOpenReadWrite);
				FileStream->Position = FileStream->Size;
				dwBytesRead = FileStream->Size;
				InternetSetFilePointer(hFile, dwBytesRead, NULL, FILE_CURRENT, 0);
				ProgressBar->Position = (dwBytesRead * 100 / dwBufLen);
				ProgressBar->Max = dwContentLen;
			}
			else
			{
				FileStream = new TFileStream(SaveDialog->FileName, fmCreate | fmShareExclusive);
				ProgressBar->Position = 0;
				ProgressBar->Max = dwContentLen;
			}

			char* Data = new char [dwBufLen];
			while(InternetReadFile(hFile, static_cast<void*>(Data), dwBufLen, &dwRead))
			{
				if(dwRead == 0)
					break;

				FileStream->Write(Data, dwRead);
				ProgressBar->Position += dwRead;
				Application->ProcessMessages();
			}

			delete[] Data; 
			if(FileStream->Size != dwContentLen)
				throw(Exception(AnsiString("File saved is incomplete")));
		}
	}
	__finally
	{
		if(SaveDialog) delete SaveDialog;
		if(FileStream) delete FileStream;
		if(hFile) InternetCloseHandle(hFile);
		if(hSession) InternetCloseHandle(hSession);
	}



Thanks

Will
Technical Details
Message-ID: <454f7d80$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newshub.sdsu.edu!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: feeec,f20f86396ab98121,start
X-Google-Attributes: gidfeeec,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Will Bright" <no@spam.com>
Sender: "Will Bright" <no@spam.com>
Reply-To: "Will Bright" <no@spam.com>
Subject: Downloading and resuming
Newsgroups: borland.public.cppbuilder.internet.web
X-User-Info: 66.61.156.67     
NNTP-Posting-Host: 207.105.83.66
Message-ID: <454f7d80$1@newsgroups.borland.com>
Date: 6 Nov 2006 10:22:56 -0700
X-Trace: newsgroups.borland.com 1162837376 207.105.83.66 (6 Nov 2006 10:22:56 -0700)
Lines: 87
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newshub.sdsu.edu!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail