Solved

CHttpFile problem

Posted on 2000-03-28
3
853 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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