Scene:
1. When programming in Windows, the download HTTP page (Html,xml) can use the WinHTTP library, but it does not download the file very well because it will fail. This leads to the WinInet library, but the stability of this library is relatively low, the use of examples and less,
Downloading large files is often incomplete and can be found with little or no special-case solutions.
2. My principle is that if the system has its own system, but it takes a lot of time for WinINet to master it. The time factor takes into account the Libcurl.
3. Libcurl support Ftp,http and other protocols such as file reading, but also automatically get the file size, the most important is not how to modify it can be stable support full download large files, but also support cross-platform (WINDOWS,MACOSX).
Refer to compiled Curl.exe using:
Curl.exe-o http://img.ptcms.csdn.net/article/201506/25/558bbe1baed6e.jpg
There are other scenarios for writing Libcurl's use before:
[libcurl]_[c/c++]_[uses Libcurl library to do simple software update solution]
Compiling the MinGW library is easy, relying directly on the Windows local library, to compile the MSVC version, you need to go to the Winbuild directory, reference BUILD. In the WINDOWS.txt.
nmake/f makefile.vc mode=<static or dll> <options>
I use the configuration
nmake/f MAKEFILE.VC Mode=dll vc=10
Http_download_domain.h
#ifndef __http_download_domain#define __http_download_domain#include <string> #include "curl/curl.h" class Httpdownloaddomain{public:httpdownloaddomain (bool* cancel); ~httpdownloaddomain (); bool DownloadFile (std::string url,std::wstring path); bool *cancel_;private:static size_t downloadcallback (void* pbuffer, size_t nSize, size_t Nmembyte, void* pparam); static int progresscallback (void *clientp, double dltotal, double Dlnow, double ultotal, double ul now);}; #endif
Http_download_domain.cpp
#include "stdafx.h" #include "http_download_domain.h" #include <iostream>httpdownloaddomain:: Httpdownloaddomain (bool* cancel) {cancel_ = cancel;} Httpdownloaddomain::~httpdownloaddomain () {}size_t httpdownloaddomain::D ownloadcallback (void* pBuffer, size_t nSize , size_t Nmembyte, void* pparam) {file* fp = (file*) pparam; size_t nwrite = fwrite (pbuffer, NSize, Nmembyte, FP); return nwrite; } int Httpdownloaddomain::P rogresscallback (void *clientp, double dltotal, double Dlnow, double ultotal, double ulnow) {httpdownloaddomain* DD = (httpdownloaddomain*) clientp; if (Dltotal > -0.1 && dltotal < 0.1) {return 0;} int nPos = (int) ((dlnow/dltotal) *100); Notification progress bar update download Progress std::cout << dltotal: << (long) dltotal << "----Dlnow:" << (long) Dlnow <&L T Std::endl;if (*dd->cancel_) {//1. Returns a value other than 0 terminates the curl_easy_perform execution return-2;} return 0; }bool Httpdownloaddomain::D ownloadfile (std::string urladdr,std::wstringPath) {//Initialize curl, this is a must for curl *curl = Curl_easy_init (); Curl_easy_setopt (Curl, Curlopt_url, urladdr.c_str ()); Set the callback to receive data file* FILE = _wfopen (Path.c_str (), L "WB"); Curl_easy_setopt (Curl, curlopt_writefunction, downloadcallback); curl_easy_setopt (curl, curlopt_writedata,file); Curl_easy_setopt (Curl, Curlopt_maxredirs, 5); Curl_easy_setopt (Curl, curlopt_followlocation, 1); Curl_easy_setopt (Curl, curlopt_noprogress, 0); Curl_easy_setopt (Curl, curlopt_progressfunction, progresscallback); curl_easy_setopt (Curl, Curlopt_progressdata, this); Curlcode Retccode = curl_easy_perform (curl); Const char* perror = Curl_easy_strerror (Retccode), Std::cout << "perror:" << perror << std::endl;fclose (file); Clean curl, and the previous initialization matches curl_easy_cleanup (curl); return!retccode;}
How to use:
#include "stdafx.h" #include "http_download_domain.h" int _tmain (int argc, _tchar* argv[]) {bool i = 0; Httpdownloaddomain HDD (&i) HDD. DownloadFile ("Http://img.ptcms.csdn.net/article/201506/25/558bbe1baed6e.jpg", L "c:\\users\\apple\\downloads\\ 558bbe1baed6e.jpg "); System ("pause"); return 0;}
Download the full example:
http://download.csdn.net/detail/infoworld/8840787
[libcurl]_[primary]_[use Libcurl to download large files]