🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Downloading and resuming

#491
From: Will Bright
Date: Tue, 07 Nov 2006 23:27
150 lines
5224 bytes
Does anything look abnormal in the code to you? I cannot seem
to get past a ertain point when testing the program, even debugging gets me nowhere.

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

Once I get here my exception message is thrown, I don't get
that one at all.  I must be missing something.

--------------------------------------------------------------

	HINTERNET hSession = NULL;
	HINTERNET hFile = NULL;
	HINTERNET hConnect = NULL;
	DWORD dwBytesRead = NULL;
	DWORD dwContentLen;
	DWORD dwBufLen = sizeof(dwContentLen);
	LPCTSTR szAcceptTypes[] = {"*.*", NULL};

	int iWritten;

	BYTE Data[1024];
	LPBYTE DataPtr;

	AnsiString AcceptRanges;
	AnsiString FileName;
	bool bIsResuming = false;

	TSaveDialog* SaveDialog;
	TFileStream* FileStream;

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

		hConnect = InternetConnect(hSession, "www.appcontrols.com",
					INTERNET_DEFAULT_HTTP_PORT, "","", INTERNET_SERVICE_HTTP, 0, 0);
		if(!hConnect)
            throw Exception("unable to create internet connect session");

		hFile = HttpOpenRequest(hConnect, "HEAD", edtURL->Text.c_str(), "HTTP/1.1",
								NULL, szAcceptTypes, INTERNET_FLAG_RELOAD, 0);
		if(!hFile)
			throw Exception("unable to create info request for the url: " + edtURL->Text);

		if(!HttpSendRequest(hFile, NULL, 0, NULL, 0))
			throw Exception("unable to send info request to the server");

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

		if(!GetHttpInfoString(hFile, HTTP_QUERY_ACCEPT_RANGES, AcceptRanges))
			throw Exception("Failed to obtain if the server supports ranges");

		InternetCloseHandle(hFile);
		hFile = HttpOpenRequest(hSession, "GET", edtURL->Text.c_str(), "HTTP/1.1",
								NULL, szAcceptTypes, INTERNET_FLAG_RELOAD, 0);
		if(!hFile)
			throw Exception("unable to create file request for the url: " + edtURL->Text);

		SaveDialog = new TSaveDialog(NULL);
		try
		{
			if(SaveDialog->Execute())
				FileName = SaveDialog->FileName;
		}
		__finally
		{
			delete SaveDialog;
		}

		if(FileName != "")
		{
			if(FileExists(FileName))
			{
				if(AcceptRanges.Pos("bytes") != 0)
				{
					AnsiString Range = ("Range: bytes=" + AnsiString(FileStream->Size) + "-\r\n");
					if(!HttpAddRequestHeaders(hFile, Range.c_str(), -1, HTTP_ADDREQ_FLAG_ADD))
						throw Exception("unable to add range header to file request");

					bIsResuming = true;
				}
			}

			if(!HttpSendRequest(hFile, NULL, 0, NULL, 0))
				throw Exception("unable to send file request to the server");

			if(bIsResuming)
				FileStream = new TFileStream(FileName, fmOpenReadWrite | fmShareDenyWrite);
			else
				FileStream = new TFileStream(FileName, fmCreate);

			try
			{
				if(bIsResuming)
				{
					FileStream->Seek(0, soFromEnd);
					dwBytesRead = FileStream->Position;
				}
				else
					dwBytesRead;

				ProgressBar->Position = (dwBytesRead * 100 / dwBufLen);
				ProgressBar->Max = dwContentLen;

				while(InternetReadFile(hFile, Data, 1024, &dwBytesRead))
				{
					if(dwBytesRead == 0)
						break;

					iWritten = 0;
					DataPtr = Data;

					do
					{
						iWritten = FileStream->Write(DataPtr, dwBytesRead);
						if(iWritten < 1)
							throw Exception("unable to write to local file");

						DataPtr += iWritten;
						dwBytesRead -= iWritten;

						ProgressBar->StepBy(iWritten);
						Application->ProcessMessages();
					}
					while(dwBytesRead != 0);
				}

				if(FileStream->Size != dwContentLen)
					throw Exception("File saved is incomplete");
			}
			__finally
			{
				delete FileStream;
			}
		}
	}
	__finally
	{
		if(hFile) InternetCloseHandle(hFile);
		if(hConnect) InternetCloseHandle(hConnect);
		if(hSession) InternetCloseHandle(hSession);
	}
Technical Details
Message-ID: <45512485$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <454f7d80$1@newsgroups.borland.com> <454fa637$1@newsgroups.borland.com> <454fb7d0$1@newsgroups.borland.com> <454fe1a9$2@newsgroups.borland.com> <454ff6ea$1@newsgroups.borland.com> <4550497c@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: feeec,f20f86396ab98121
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: Re: Downloading and resuming
Newsgroups: borland.public.cppbuilder.internet.web
X-User-Info: 207.126.223.2     
References: <454f7d80$1@newsgroups.borland.com> <454fa637$1@newsgroups.borland.com> <454fb7d0$1@newsgroups.borland.com> <454fe1a9$2@newsgroups.borland.com> <454ff6ea$1@newsgroups.borland.com> <4550497c@newsgroups.borland.com>
NNTP-Posting-Host: 207.105.83.66
Message-ID: <45512485$1@newsgroups.borland.com>
Date: 7 Nov 2006 16:27:49 -0700
X-Trace: newsgroups.borland.com 1162945669 207.105.83.66 (7 Nov 2006 16:27:49 -0700)
Lines: 151
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail