🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.web
21 messages
21 total messages Started by Will Bright Mon, 06 Nov 2006 17:22
Downloading and resuming
#733
Author: 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
Re: Downloading and resuming
#486
Author: Will Bright
Date: Mon, 06 Nov 2006 20:47
11 lines
1151 bytes
>Why not?  Please be more specific.  What EXACTLY do you see happening?

Basically all I was getting was disconnects without any more
data being downloaded.  I assumed that there was something
wrong somewhere in the code.

Thanks for the examples, I will test it out now and I am hoping
it works perfectly. I am tired of messing with this code.

Will
Re: Downloading and resuming
#735
Author: Remy Lebeau (TeamB)
Date: Mon, 06 Nov 2006 21:14
221 lines
8072 bytes
"Will Bright" <no@spam.com> wrote in message
news:454f7d80$1@newsgroups.borland.com...

> I am trying to resume the download so it can continue on, but
> my code does not seem to be doing just that.

Why not?  Please be more specific.  What EXACTLY do you see happening?

> InternetSetFilePointer(hFile, dwBytesRead, NULL, FILE_CURRENT, 0);

You are not doing any error checking to ensure that operation is succeeding.
Not all servers support resuming.  For those that don't,
InternetSetFilePointer() invokes some pretty bad performance penalties as
the original data is re-downloaded from the beginning and simply discarded
until the resume position is reached.

There is a more efficient way to handle resuming on a server that supporting
resuming - use HttpAddRequestHeaders() to add a 'Range' header to the
request, specifying the bytes you want.  You will have to change your code
to use HttpOpenRequest() instead of InternetOpenURL(), though.

> FileStream->Write(Data, dwRead);

Write() returns the actual number of bytes written.  You are not verifying
that the number of bytes written actually matches the number of bytes read
from the connection.

> ProgressBar->Position += dwRead;

You cannot use compund assignment operators with VCL properties.  You must
use the '=' and '+' operators separately.

    ProgressBar->Position = ProgressBar->Position + dwRead;

Or simply:

    ProgressBar->StepBy(dwRead);

With that said, try something more like the following (untested):

    bool GetHttpInfoString(HINTERNET hInfo, DWORD dwValue, AnsiString &Data)
    {
        DWORD dwLength = 0;
        DWORD dwIndex = 0;

        Data = "";

        HttpQueryInfo(hInfo, dwValue, NULL, &dwLength, &dwIndex);
        if( GetLastError() != ERROR_INSUFFICIENT_BUFFER )
            return false;

        LPTSTR buffer = (LPTSTR) LocalAlloc(LPTR, dwLength+1);
        if( !buf )
            return false;

        dwIndex = 0;
        if( !HttpQueryInfo(hInfo, dwValue, buffer, &dwLength, &dwIndex) )
        {
            LocalFree(buffer);
            return false;
        }

        try
        {
            Data = buffer;
        }
        catch(const Exception &)
        {
            LocalFree(buffer);
            return false;
        }

        LocalFree(buffer);
        return true;
    }

    {
        HINTERNET hSession = NULL;
        HINTERNET hFile = NULL;
        DWORD dwContentLen;
        DWORD dwBufLen = sizeof(dwContentLen);
        DWORD dwRead;
        LPTSTR* 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 to create internet session");

            hFile = HttpOpenRequest(hSession, "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, &dwRead) )
                    {
                        if( dwRead == 0 )
                            break;

                        iWritten = 0;
                        DataPtr = Data;

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

                            DataPtr += iWritten;
                            dwRead -= iWritten;

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

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


Gambit
Re: Downloading and resuming
#487
Author: Will Bright
Date: Mon, 06 Nov 2006 21:31
11 lines
1130 bytes
	LPTSTR* szAcceptTypes = {"*.*", NULL};

You got me on that line.

[C++ Error] Unit1.cpp(63): E2291 } expected
[C++ Error] Unit1.cpp(63): E2034 Cannot convert 'char *' to 'char * *'
[C++ Error] Unit1.cpp(63): E2141 Declaration syntax error


Thanks
Re: Downloading and resuming
#488
Author: Remy Lebeau (TeamB)
Date: Tue, 07 Nov 2006 01:22
18 lines
1332 bytes
"Will Bright" <no@spam.com> wrote in message
news:454fb7d0$1@newsgroups.borland.com...

> LPTSTR* szAcceptTypes = {"*.*", NULL};
>
> You got me on that line.
>
> [C++ Error] Unit1.cpp(63): E2291 } expected
> [C++ Error] Unit1.cpp(63): E2034 Cannot convert 'char *' to 'char * *'
> [C++ Error] Unit1.cpp(63): E2141 Declaration syntax error

Try this:

    LPTSTR szAcceptTypes[] = {"*.*", NULL};


Gambit
Re: Downloading and resuming
#489
Author: Will Bright
Date: Tue, 07 Nov 2006 02:00
22 lines
1563 bytes
>Try this:
>
>    LPTSTR szAcceptTypes[] = {"*.*", NULL};


I have it boiled down to about two errors now.

		hFile = HttpOpenRequest(hSession, "HEAD", edtURL->Text.c_str(), "HTTP/1.1",
								NULL, szAcceptTypes, INTERNET_FLAG_RELOAD, 0);
[C++ Error] Unit1.cpp(87): E2034 Cannot convert 'char * *' to 'const char * *'
[C++ Error] Unit1.cpp(87): E2342 Type mismatch in parameter 'lplpszAcceptTypes' (wanted 'const char * *', got 'char * *')

I had a problem with one of the variables, but I think it was
just not given the correct name.  I will see if it works when
I finally get it all to compile.


Thanks again


Will
Re: Downloading and resuming
#490
Author: Remy Lebeau (TeamB)
Date: Tue, 07 Nov 2006 08:50
19 lines
1506 bytes
"Will Bright" <no@spam.com> wrote in message
news:454ff6ea$1@newsgroups.borland.com...

> hFile = HttpOpenRequest(hSession, "HEAD", edtURL->Text.c_str(),
"HTTP/1.1",
> NULL, szAcceptTypes, INTERNET_FLAG_RELOAD, 0);
> [C++ Error] Unit1.cpp(87): E2034 Cannot convert 'char * *' to 'const char
* *'

    LPCTSTR szAcceptTypes[] = {"*.*", NULL};

Or:

    LPTSTR szAcceptTypes[] = {"*.*", NULL};
    hFile = HttpOpenRequest(..., (LPCTSTR) szAcceptTypes, ...);


Gambit
Re: Downloading and resuming
#491
Author: 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);
	}
Re: Downloading and resuming
#492
Author: Remy Lebeau (TeamB)
Date: Wed, 08 Nov 2006 00:29
15 lines
1510 bytes
"Will Bright" <no@spam.com> wrote in message
news:45512485$1@newsgroups.borland.com...

> 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

Have you tried calling GetLastError() yet to find out why it is failing?


Gambit
Re: Downloading and resuming
#493
Author: Will Bright
Date: Wed, 08 Nov 2006 03:09
24 lines
2032 bytes
>Have you tried calling GetLastError() yet to find out why it is failing?

Apparently my hFile handle seems to be invalid after I get
here at the HttpAddRequestHeaders() func.

AnsiString Range = ("Range: bytes=" + AnsiString(FileStream->Size) + "-\r\n");
if(!HttpAddRequestHeaders(hFile, Range.c_str(), -1, HTTP_ADDREQ_FLAG_ADD))
ListBox1->Items->Add("HttpAddRequestHeaders() " + SysErrorMessage(GetLastError()));


It doesn't make sense, the handle was closed here \/

InternetCloseHandle(hFile);
hFile = HttpOpenRequest(hSession, "GET", edtURL->Text.c_str(), "HTTP/1.1",
	NULL, szAcceptTypes, INTERNET_FLAG_RELOAD, 0);

but the hSession was used to do the InternetConnect() and then
used by the HttpOpenRequest().  I will have to debug a bit more
because I just don't see right now how the handle is becoming
invalid.


Will
Re: Downloading and resuming
#494
Author: Remy Lebeau (TeamB)
Date: Wed, 08 Nov 2006 05:21
31 lines
2250 bytes
"Will Bright" <no@spam.com> wrote in message
news:45515872$1@newsgroups.borland.com...

> Apparently my hFile handle seems to be invalid after I get
> here at the HttpAddRequestHeaders() func.

That is not true.  There is nothing to invalidate the handle between the
time it is created by HttoOpenRequest() and the time the range header is
added by HttpAddRequestHeaders().  Especially since the handle is not used
for anything in between those two operations.

You said the error was coming from HttpQueryInfo().  Are you changing your
mind now?

> the hSession was used to do the InternetConnect() and then
> used by the HttpOpenRequest().

Which is perfectly fine.

I will have to debug a bit more
> because I just don't see right now how the handle is becoming
> invalid.

It can't be.  Your error has to be something else.  Where EXACTLY is the
error actually occuring?  You said one thing earlier, and now you are saying
something different.  And what EXACTLY does GetLastError() actually return
when the error does occur?


Gambit
Re: Downloading and resuming
#497
Author: Will Bright
Date: Wed, 08 Nov 2006 12:44
36 lines
2152 bytes
This is exactly where my error message is coming from.

if(!HttpSendRequest(hFile, NULL, 0, NULL, 0))
{
  ListBox1->Items->Add("HttpSendRequest() " + SysErrorMessage(GetLastError()));
  throw Exception("unable to send file request to the server");
}


The reason I got further in the code and gave other messages,
I had commented out the "throw Exception()" calls to just
have it display the message in the listbox.

The message I get back now is "unable to send file request to
the server" but my GetLastError() shows no messages in the
listbox.

Can you confirm these two items for me, I keep wondering if
they are correct or not.

In the InternetOpen() call, is the URL supposed to be the way
I have it here?  
InternetConnect hSession, "www.appcontrols.com",

Also in the HttpOpenRequest(), my edtURL->Text is pointing to
this url: http://www.appcontrols.com/download/cronjob.zip
is the format correct?

HttpOpenRequest(hConnect, "HEAD", edtURL->Text.c_str(),

I will have to keep tracing and tsting, I don't get it at the
moment.

  
Will
Re: Downloading and resuming
#496
Author: Will Bright
Date: Wed, 08 Nov 2006 12:50
15 lines
1588 bytes
This error seems to be the most consistent

if(!HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
						 (LPVOID)&dwContentLen, &dwBufLen, 0))
		{
			ListBox1->Items->Add("HttpQueryInfo() " + SysErrorMessage(GetLastError()));
			throw Exception("Failed to obtain the length of the file");
		}

GetLastError() gives me nothing in my listbox, but the message
"Failed to obtain the length of the file" I do get that one.


Will
Re: Downloading and resuming
#499
Author: Remy Lebeau (TeamB)
Date: Wed, 08 Nov 2006 20:03
36 lines
2380 bytes
"Will Bright" <no@spam.com> wrote in message
news:4551df26@newsgroups.borland.com...

> This is exactly where my error message is coming from.

Again, I ask you to PLEASE provide the actual error information that is
being reported.  This is the third time now that I have asked you for that.

> The message I get back now is "unable to send file request to
> the server" but my GetLastError() shows no messages in the
> listbox.

GetLastError() is guaranteed to return a valid error code if any of the
WinInet functions return FALSE.

> In the InternetOpen() call, is the URL supposed to be the way
> I have it here?

You are not supposed to pass any URL to InternetOpen() to begin with.  The
original code you showed, and the code I gave you afterwards, are not doing
that.  Did you change it?

> InternetConnect hSession, "www.appcontrols.com",

Why are you calling InternetConnect()?  The code I gave you was not doing
that.

> Also in the HttpOpenRequest(), my edtURL->Text is pointing to
> this url: http://www.appcontrols.com/download/cronjob.zip
> is the format correct?

Yes.


Gambit
Re: Downloading and resuming
#500
Author: Remy Lebeau (TeamB)
Date: Wed, 08 Nov 2006 20:04
10 lines
1491 bytes
"Will Bright" <no@spam.com> wrote in message
news:4551e0b0$1@newsgroups.borland.com...

> GetLastError() gives me nothing in my listbox

Again, that is not possible.  It has to be giving you something.


Gambit
Re: Downloading and resuming
#514
Author: Will Bright
Date: Wed, 08 Nov 2006 23:03
39 lines
2642 bytes
> GetLastError() is guaranteed to return a valid error code if any of the
> WinInet functions return FALSE.

I'm telling you the truth, GetLastError() is returning nothing at all.

> Why are you calling InternetConnect()?  The code I gave you was not doing
> that.

I used InternetConnect() because in the original code I could not get past 
the
HttpOpenRequest() call.  Then I looked at the function for that call and it
claimed that you needed a handle from InternetConnect().  That's when I
put InternetConnect() in there.

HttpOpenRequest()
HINTERNET WINAPI HttpOpenRequest(
  HINTERNET hConnect,
  LPCTSTR lpszVerb,
  LPCTSTR lpszObjectName,
  LPCTSTR lpszVersion,
  LPCTSTR lpszReferrer,
  LPCTSTR* lplpszAcceptTypes,
  DWORD dwFlags,
  DWORD dwContext
);Parameters
  hConnect
  [in] Handle to an HTTP session returned by InternetConnect.
Maybe you will see something that I am overlooking, I'm telling you I just 
don't get this at all.
No errors returned from GetLastError() is really weird.  I could understand 
if it said something
like overlapped operation in progress, but I am not even getting that.

I am uploading the sample to the attachments dir, if you can, please look at 
it. There is nothing
in there except for the fucntion calls to download the file.


Will
Re: Downloading and resuming
#516
Author: Will Bright
Date: Thu, 09 Nov 2006 00:00
12 lines
1529 bytes
I was finally able to get an error message using this instead.

TListItem* Item = ListView1->Items->Add();
Item->Caption = "HttpQueryInfo()";
Item->SubItems->Add(dwErrorCode);

My error is 12150 under HttpQueryInfo().  I guess some research
is in order now to figure out the meaning of that error message.


Will
Re: Downloading and resuming
#515
Author: Remy Lebeau (TeamB)
Date: Thu, 09 Nov 2006 00:06
25 lines
2172 bytes
"Will Bright" <no@spam.com> wrote in message
news:45526262@newsgroups.borland.com...

> I'm telling you the truth, GetLastError() is returning nothing at all.

So, you are saying that it is returning 0 every time?  Have you tried
calling GetLastError() BEFORE calling any other VCL code?  For example:

    if(!HttpSendRequest(hFile, NULL, 0, NULL, 0))
    {
        DWORD dwErrorCode = GetLastError(); // <-- cannot return 0!
        ListBox1->Items->Add("HttpSendRequest() " +
SysErrorMessage(dwErrorCode));
        throw Exception("unable to send file request to the server");
    }

> I used InternetConnect() because in the original code I could not get
> past the HttpOpenRequest() call.  Then I looked at the function for that
> call and it claimed that you needed a handle from InternetConnect().

Ok.


Gambit
Re: Downloading and resuming
#518
Author: Will Bright
Date: Thu, 09 Nov 2006 02:04
11 lines
1678 bytes
error).  What is
>likely happening is that the error is sending some other value for the
>status code, and your code is not set up to handle it yet.

Yea, that is my problem.  I guess I will do a bit of searching
to see if I can find some samples out there that shows how to
handle the error codes.  I am too close to give up now.....


Will
Re: Downloading and resuming
#517
Author: Remy Lebeau (TeamB)
Date: Thu, 09 Nov 2006 02:10
17 lines
2111 bytes
"Will Bright" <no@spam.com> wrote in message
news:45527dc0$1@newsgroups.borland.com...

> My error is 12150 under HttpQueryInfo().

12150 is ERROR_HTTP_HEADER_NOT_FOUND, which is defined in wininet.h.  So the
server is not sending a "Content-Length" header in tis response to begin
with.  After calling HttpSendRequest(), you should be using HttpQueryInfo()
to retreive HTTP_QUERY_STATUS_CODE, and then verify that the value is 200
(sending the whole file) or 206 (sending a portion of the file).  Any other
value is not a download (though not every value is an error).  What is
likely happening is that the error is sending some other value for the
status code, and your code is not set up to handle it yet.


Gambit
Re: Downloading and resuming
#519
Author: Remy Lebeau (TeamB)
Date: Thu, 09 Nov 2006 05:11
14 lines
1808 bytes
"Will Bright" <no@spam.com> wrote in message
news:45529ab3$1@newsgroups.borland.com...

> Yea, that is my problem.  I guess I will do a bit of searching
> to see if I can find some samples out there that shows how to
> handle the error codes.  I am too close to give up now.....

I suggest you read RFC 2616 (http://www.ietf.org/rfc/rfc2616.txt).  Sections
6 explains how HTTP responses work, and section 10 explains how status codes
work.


Gambit
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads