intelliproject logo

Location: Desktop development - C/C++    License: The Intelliproject Open License (IPOL)

Download a remote file from a valid URL in C++

Posted by Silviu Caragea

You can use this code to download a remote file from a valid URL using API functions or MFC classes

Skill: Intermediate

Posted: 02/11/2008

Views: 905

Rating: 0.00 /5

Popularity: 0.00

Sign Up to vote for this article

Introduction

Sometimes you need to download the content of a remote file into a buffer for subsequent processing.

You can do this in many ways:

  • Using low level sockets functions.
  • Using API functions like InternetReadFile which reads data from a handle opened by the InternetOpenUrl, FtpOpenFile, GopherOpenFile, or HttpOpenRequest function.
  • Using other specialized classes from MFC like CInternetSession.

The goal of this  article is to describe the last 2 methods mentioned above.

Download a remote file from a valid URL using API

API functions used above:

  • InternetOpen - Initializes an application's use of the WinINet functions.
  • InternetOpenUrl - Opens a resource specified by a complete FTP, Gopher, or HTTP URL.
  • HttpQueryInfo - Retrieves header information associated with an HTTP request.
  • InternetReadFile - Reads data from a handle opened by the InternetOpenUrl, FtpOpenFile, GopherOpenFile, or HttpOpenRequest function.
  • InternetCloseHandle - Closes a single Internet handle.

Download a remote file from a valid URL using CInternetSession class

As follows I will describe a wrapper class which will incapsulate the CInternetSession functionality .To use this class you should include afxinet.h (#include <afxinet.h>)

Using this code

1. Using method based on API functions

2. Using method based on MFC classes

License

This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)

About the author

Silviu Caragea

Silviu Caragea is the Founder, Administrator and Chief Editor who wrote and runs The IntelliProject.

He's been programming since 2000 and now he's student at The Faculty of Economic Cybernetics, Statistics and Informatics from Bucharest. In the same time he's working as software developer at Cratima Software, a Romanian software and web design company that activates both on the local and foreign market, providing its customers with software development services, internet and intranet solutions, web design, graphic design and IT consultancy.

His programming experience includes:
- C,C++, Visual C++(Win32 API, MFC, ADO, STL, DAO, ODBC, ATL, COM, DirectShow, DirectDraw, WTL)
- Open Source libraries :CURL & Boost
- HTML, CSS
- Java (SE,ME)
- JavaScript, Ajax, Google Web Toolkit (GWT)
- Php, MySQL
-Oracle, PL SQL
- C# .NET
-Objective C, IPhone SDK, Cocoa

Location: Romania
Ocupation: Software Engineer
Home page: http://www.intelliproject.net

Sign up to post message on the article message board!