Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CHttpFile problem

Posted on 2000-03-28
3
Medium Priority
?
869 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
mikeblas earned 400 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 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