Simple C, C++ Program to download a file from internet URL

This is a ten line C, C++ program that connects to the internet and downloads the html source of a web page. The given program supports both http and https requests and works even if there is a url redirect from one protocol to the other, ie., from http-to-https or from https-to-http.

Categories | About |     |  

Parveen,

We have shown two methods and each supports both the http and https protocols. As of the date of publishing of this article, we have tested that the programs support redirects also, from http to https, and vice-versa.

IMPORTANT NOTE: ONLY FOR WINDOWS. NEEDS A COMPILER WITH WINDOWS SET UP. COMPILE USING Visual Studio C++, Visual C++ Express, any edition, any version

Method 1 - Save to a File

This method is based on the Win32 API URLDownloadToFile. It downloads a URL to a file on your PC. The file is saved in the same working directory where the program is run.

// ONLY FOR WINDOWS 
// NEEDS WINDOWS SET UP 
// COMPILE USING Visual Studio Express, 
// Visual C++ Express or any edition 
// any version 
#include <windows.h>

#include <cstdio>

#pragma comment(lib, "Urlmon.lib")

using namespace std;

// Program to download the home page 
// of google.com to a file named 
// myfile.txt. the file can be 
// found in the same working directory 
// from which the project runs 
int main()
{

  // the URL to download from 
  const wchar_t* srcURL = L"https://google.com";

  // the destination file 
  const wchar_t* destFile = L"myfile.txt";

  // URLDownloadToFile returns S_OK on success 
  if (S_OK == URLDownloadToFile(NULL, srcURL, destFile, 0, NULL))
  {

    printf("Saved to 'myfile.txt'");

    return 0;

  }

  else 
  {

    printf("Failed");

    return -1;

  }

}

Method 2 - Save to a String

This program is a bit lengthy. The program collects the bytes to a string, but can be easily modified to save the bytes to a file.

  1. Use the URLOpenBlockingStream Win32 API to obtain an IStream interface to the web url.
  2. The second parameter is the URL address. http:// and https:// both protocols work. The other four are usual boilerplate defaults. If this function succeeds, it returns 0, otherwise, a non-zero error code.
  3. Once an IStream is available, the "Read" function can be used in a loop to download the bytes.
  4. The bytes are collected in a string. This is not a very efficient way of building a long string. I have used it to keep the code readable. See the stackoverflow reference below for a better method.
// ONLY FOR WINDOWS 
// NEEDS WINDOWS SET UP 
// COMPILE USING Visual Studio Express, 
// Visual C++ Express or any edition 
// any version 
#pragma comment(lib, "urlmon.lib")

#include <urlmon.h>

#include <cstdio>

#include <iostream>

#include <string>

#define getURL URLOpenBlockingStreamA

using namespace std;

// c program to download a file from url 
int main()
{

  // Windows IStream interface 
  IStream* stream;

  const char* URL = "http://google.com";

  // make a call to the URL 
  // a non-zero return means some error 
  if (getURL(0, URL, &stream, 0, 0))
  {

    cout << "Error occured.";

    cout << "Check internet";

    cout << "Check URL. Is it correct?";

    return -1;

  }

  // this char array will be cyclically 
  // filled with bytes from URL 
  char buff[100];

  // we shall keep appending the bytes 
  // to this string 
  string s;

  unsigned long bytesRead;

  while(true)
  {

    // Reads a specified number of bytes 
    // from the stream object into char 
    // array and stores the actual 
    // bytes read to "bytesRead" 
    stream->Read(buff, 100, &bytesRead);

    if(0U == bytesRead)
    {

      break;

    }

    // append and collect to the string 
    s.append(buff, bytesRead);

  };

  // release the interface 
  // good programming practice 
  stream->Release();

  // display 
  cout << s << endl;

  return 0;

}

// program kept small for clarity 
// may not be efficient 

Comments and Discussion

Please write your comments and discussion on the following youtube video itself:


This Blog Post/Article "Simple C, C++ Program to download a file from internet URL" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.