Use Libcurl to download files for small cases

Source: Internet
Author: User

Libcurl is a powerful open source network processing library that supports HTTP, HTTPS, FTP ... A range of network protocols. Using it for HTTP get\post or download files is a piece of cake, the chrome kernel has used it, this article mainly on a use Curl download file of the small example.

The first is to download the latest Curl source code, and then compile into a dynamic library or a static library, and then put the head files and library files to join our own project, the reference statement:

#include "curl.h" #ifdef _debug#pragma comment (lib, "... /debug/libcurld ") #else #pragma comment (lib," ... /release/libcurl ") #endif
Deliberately added some comments on the original code, hoping to better understand the code:
<pre name= "code" class= "CPP" >//UseLibCurl.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <Windows.h> #include <CommCtrl.h> #include "resource.h" #include "curl.h" # ifdef _debug#pragma Comment (lib, "... /debug/libcurld ") #else #pragma comment (lib," ... /release/libcurl ") #endif//The window handle that is used to save the dialog box, because the window handle to which you want to send a message, you must know its window handles Hwndg_hdlgwnd = null;//provide to curl download progress callback function, Used to save downloaded data to a file static Size_tdownloadcallback (void* pbuffer, size_t nSize, size_t nmembyte, void* pparam);// A function provided to curl download progress callback for calculating the download progress notification interface, static int progresscallback (void *clientp, double dltotal, double Dlnow, double ultotal, Double ulnow);//This is the message loop of the dialog box, creating the GUI inside the console program, just to better show the download callback function INT_PTR CALLBACK DialogProc (HWND hwnd, UINT umsg, WPARAM WParam, LPARAM LPARAM);//This is the download of the thread function, in order not to put the main interface of the dialog box to die, it must be the user to download the DWORD WINAPI downloadthread (LPVOID lpparam); int _ Tmain (int argc, _tchar* argv[]) {//Popup dialog box, knowing that the dialog closes before execution exits dialogbox (null, Makeintresource (IDD_DIALOG1), NULL, DIALOGPROC) ; return 0;} Static size_t Downloadcallback (void* pbuffer, size_t nSize, size_t nmembyte, void* pparam) {//Append the downloaded data to the file (must have a, otherwise the previously written content will be overwritten) file* fp = null;fopen_s (&AMP;FP, "c:\\ test.apk "," ab+ "); size_t nwrite = fwrite (pbuffer, NSize, Nmembyte, FP); fclose (FP); return nwrite;} static int progresscallback (void *clientp, double dltotal, double Dlnow, double ultotal, double ulnow) {if (Dltotal >- 0.1 && Dltotal < 0.1) return 0;int NPos = (int) ((dlnow/dltotal) *100);//Notification progress bar update download progress::P ostmessage (G_hdlgwnd, W M_user +, NPos, 0);//::sleep; return 0;} INT_PTR CALLBACK DialogProc (HWND hwnd, UINT umsg, WPARAM WPARAM, LPARAM LPARAM) {switch (umsg) {case WM_INITDIALOG:{G_HDLGW nd = hWnd; hwnd hprogress = GetDlgItem (hwnd, IDC_PROGRESS1); SendMessage (Hprogress, Pbm_setrange32, (WPARAM) 0, (LPARAM) 100);//When the dialog box is initialized, create a download thread handle hthread = CreateThread (NULL, 0, Downloadthread, 0, 0, NULL); CloseHandle (Hthread);:: SetWindowText (HWnd, L "Example of downloading files using curl:"); return TRUE;} Case Wm_command:{word msg = HiWord (WParam); WORD id = loword (wParam); if (id = = IDOK | | id= = IDCANCEL) EndDialog (hWnd, id); break;} Case Wm_erasebkgnd:return true;case Wm_ctlcolorstatic:return (INT_PTR) (hbrush):: Getstockobject (WHITE_BRUSH); case WM _user + 110:{//received a message setting Progress hwndhprogress= GetDlgItem (hWnd, IDC_PROGRESS1); Hwndhstatus= GetDlgItem (HWnd, Idc_status), if (hprogress) SendMessage (hprogress, Pbm_setpos, WParam, 0L); if (hstatus) {W CHAR szbuffer[100] = {0};if (wparam<100) swprintf (szbuffer, l "Downloading file, Progress:%d%%", WParam); elseswprintf (Szbuffer, L " File Download complete! ");:: SetWindowText (Hstatus, szbuffer);} return 0;} Default:break;} Return DefWindowProc (HWnd, umsg, WParam, LParam);} DWORD WINAPI downloadthread (lpvoid lpparam) {//Initialize curl, this is required curl* curl = Curl_easy_init (); curl_easy_setopt (Curl, Curlopt_url, "http://android.shoujids.com/software/download?id=154103");//Set callback curl_easy_setopt to receive data (curl, Curlopt_writefunction, Downloadcallback);//curl_easy_setopt (Curl, curlopt_infilesize, lfilesize);//curl_easy_ Setopt (Curl, Curlopt_header, 1);//curl_easy_setopt (Curl, curlopt_nobody, 1);//curl_easy_setopt (Curl, curlopt_nosignal, 1);//sets the maximum number of redirects curl_easy_setopt (Curl, Curlopt_maxredirs, 5);//Set 301, 302 Jump Follow locationcurl_easy_setopt (curl, curlopt_followlocation, 1); curl_easy_setopt (curl, curlopt_noprogress, 0);// Set the progress callback function curl_easy_setopt (curl, curlopt_progressfunction, progresscallback);//curl_easy_getinfo (Curl, CURLINFO_ Content_length_download, &lfilesize);//curl_easy_setopt (Curl, Curlopt_progressdata, g_hDlgWnd);// Start execution request Curlcode Retccode = curl_easy_perform (curl);//See if there is an error message const char* perror = Curl_easy_strerror (Retccode);// Clean curl, and the previous initialization matches curl_easy_cleanup (curl); return 0;}


Run:


Use Libcurl to download files for small cases

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.