libcurl使用示例

简介: 远程下载文件,并将http 头信息存放内存中以及文件大小等相关信息: 1 #include 2 #include 3 #include 4 #include 5 6 struct MemoryStruct { 7 char* memory; 8 ...

远程下载文件,并将http 头信息存放内存中以及文件大小等相关信息:

 1 #include <stdio.h>
 2 #include <curl/curl.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 
 6 struct MemoryStruct {
 7     char* memory;
 8     size_t allsize;
 9 };
10 
11 static size_t WriteMemoryCallback(void* contents, size_t _size, size_t nmemb, void* userp)
12 {
13     size_t realsize = _size * nmemb;
14     struct MemoryStruct *mem = (struct MemoryStruct*)userp;
15     
16     mem->memory = (char*)realloc(mem->memory, mem->allsize + realsize + 1);
17     if(mem->memory == NULL){
18         printf("realloc error...\n");
19         return 0;
20     }
21 
22     memcpy(&(mem->memory[mem->allsize]), contents, realsize);
23     mem->allsize += realsize;
24     mem->memory[mem->allsize] = 0;
25 
26     return realsize;
27 }
28 
29 
30 size_t write_data(char* buffer, size_t size, size_t items, void* outstream)
31 {
32     int written = fwrite(buffer, size, items, (FILE*)(outstream));
33     return written;
34 }
35 
36 double get_download_size(char* url){
37     CURL* curl;
38     CURLcode res;
39     double size = 0.0;
40     int httpcode=0;
41     FILE* fd = fopen("./aaa.txt", "wb+");
42     char *type = (char*)malloc(32);
43     struct MemoryStruct chunk;
44     chunk.memory = (char*)malloc(1);
45     chunk.allsize = 0;
46         
47 
48 
49     curl = curl_easy_init();
50     curl_easy_setopt(curl, CURLOPT_URL, url);
51     //curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
52     curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0);    //不跳转
53     curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 3000);
54     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)fd);
55     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
56 
57     curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, WriteMemoryCallback);
58     curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void*)&chunk);
59 
60     res = curl_easy_perform(curl);
61     if(res != CURLE_OK){
62         fprintf(stderr, "curl_easy_getinfo() failed: %s\n", curl_easy_strerror(res));
63     }
64     res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpcode);
65     if(res != CURLE_OK || httpcode != 200 ){
66         fprintf(stdout, "httpcode error!\n");        
67     }
68     res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &size);
69     if(res != CURLE_OK ){
70         fprintf(stdout, "httpcode xxxerror!\n");        
71     }
72     
73     res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &type);
74     if(res != CURLE_OK ){
75         fprintf(stdout, "TYPE xxxerror!\n");        
76     }
77     printf("type:\n%s\n", type);
78     printf("header:\n%s\n", chunk.memory);
79     fclose(fd);
80     free(chunk.memory);    
81     curl_easy_cleanup(curl);
82 
83     return size;
84 }
85 
86 int main(int argc, char* argv[])
87 {
88     char url[] = "http://www.fastcgi.com/dist/fcgi.tar.gz";
89   
90     double filesize = get_download_size(url);
91     printf("[%0.0lf] %s\n", filesize, url);
92     return 0;
93 }

 

相关文章
|
JSON 网络协议 数据格式
curl常用参数详解及示例
curl是一个开源的命令行工具,它基于网络协议,对指定URL进行网络传输,得到数据后不任何具体处理(如:html的渲染等),直接显示在"标准输出"(stdout)上。
2967 0
|
存储 网络安全 C++
C++ LibCurl 库的使用方法
LibCurl是一个开源的免费的多协议数据传输开源库,该框架具备跨平台性,开源免费,并提供了包括`HTTP`、`FTP`、`SMTP`、`POP3`等协议的功能,使用`libcurl`可以方便地进行网络数据传输操作,如发送`HTTP`请求、下载文件、发送电子邮件等。它被广泛应用于各种网络应用开发中,特别是涉及到数据传输的场景。
386 0
|
域名解析 Kubernetes 开发工具
执行yum install ntpdate ‐y报错Could not resolve host: download.example; 未知的名称或服务
执行yum install ntpdate ‐y报错Could not resolve host: download.example; 未知的名称或服务
743 0
|
应用服务中间件 网络安全 数据安全/隐私保护
|
C++ Windows
64bit libcurl.lib生成步骤及注意事项详解
1、[前提]编译32bit libcurl.lib 参考文档:Using-libcurl-with-SSH-support-in-Visual-Studio-2010.pdf。下载地址:http://curl.haxx.se/libcurl/c/
438 0
64bit libcurl.lib生成步骤及注意事项详解
|
C语言 C++ Perl
如何处理错误消息Please install the gcc make perl packages
如何处理错误消息Please install the gcc make perl packages
160 0
|
测试技术 C++ Windows
Windows下VS2015编译并使用libcurl.lib静态库(curl:http客户端库c++版本)
Windows下VS2015编译并使用libcurl.lib静态库(curl:http客户端库c++版本)
1206 0
|
应用服务中间件
|
JSON 数据格式