Solved

CHttpFile problem

Posted on 2000-03-28
3
852 Views
Last Modified: 2013-11-20
I'd like to write a program to download files using CHttpFile. I have no problem downloading text files (html, txt). But when trying to download binary file (like jpg, gif), I can only download a fraction of it. Here is the relevant code (error checking code has been omitted):


CWinApp* pApp = ::AfxGetApp();
CInternetSession session(::AfxGetAppName(), 1, PRE_CONFIG_INTERNET_ACCESS);
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
LPCTSTR rtype = "*/*";
CString strServerName = "www.myserver.com";
CString strObject = "test.jpg";
INTERNET_PORT nPort = 80;
pServer = session.GetHttpConnection(strServerName, nPort);                  
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject, NULL, 1, &rtype, NULL, INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT);
pFile->SendRequest();
char sz[1024];
CString filename = pFile->GetFileName();
ofstream filestream((LPCTSTR)filename, ios::out, ios::binary);
while (pFile->Read(sz, 1023))
{
      filestream << sz;
}
cout << (LPCTSTR)filename;
                  
filestream.close();
pFile->Close();
pServer->Close();
0
Comment
Question by:wyldsj
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
mikeblas earned 100 total points
ID: 2666647

The immediate problem is that you're misusing filestream. The put-to operator takes a null-terminated string, but you want to write a block of bytes. So, what ends up happening is that you read 1023 bytes from your internet connection and then write only up to the first null terminated character in that block.

Further, your constructor to build filestream is wrong. You need to or together the ios::values you use. Passing ios::binary for the 3rd parameter is bogus. The third parameter is for protection flags, not file type flags.

So, the fixed code looks like this:

   ofstream filestream((LPCTSTR) filename, ios::out | ios::binary);

   int nRead;
   while ( (nRead = pFile->Read(sz, 1023)) > 0)
   {
      filestream.write(sz, nRead);
   }

And that fixes your problem.

There's lots more I'd call "wrong" with your code, though.

Why are you using ofstream in the first place? CFile is already linked into your application, and it would do this job in a grand fashion. In fact, you'll find it to be far more efficient than ofstream in this application. You should use it, instead.

More glaringly, you don't catch any exectpions. All of the functions you're calling in MFC, and even those in ofstream, can throw exceptions. Essentially, this is as bad as not checking error return codes.

..B ekiM
0
 

Author Comment

by:wyldsj
ID: 2667543
Thanks so much mikeblas! This is the best answer I've ever got in expert-exchange :) I learnt much more than the question itself.
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2669670
Thank you for your kind words.  It's easy to provide clear help in response to a concise and specific question.

..B ekiM
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

22 Experts available now in Live!

Get 1:1 Help Now