libcurl使用心得-包括下载文件不存在处理相关(转)
libcurl使用心得
Libcurl为一个免费开源的,客户端url传输库,支持FTP,FTPS,TFTP,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE和LDAP,跨平台,支持Windows,Unix,Linux等,线程安全,支持Ipv6。并且易于使用。
从http://curl.haxx.se/libcurl/ 下载一个稳定的版本,注意选择OS。
在使用之前请大家多阅读libcurl的文档:因为如果要实际运用到项目中,最好对libcurl有具体的了解,具体在
http://curl.haxx.se/libcurl/c/
curl_easy_setopt()
curl_easy_perform()
curl_easy_getinfo()
这三个函数的使用上,需要多去钻研,多看Samples,你才能灵活使用libcurl。
感谢这篇文章:
http://blog.163.com/xu_chao2000/blog/static/27770610200801303252802/
给了我许多启发,再次感谢!
给出我的一个简单的代码例子:
说明:
1.关键在curl_easy_setopt函数设置option,可以设置ftp,http,get,post等许多选项,请根据具体使用情况设置。
2.对取回来的数据需要进行判断,比如http下载文件,如果文件不存在,需要进行处理。因为writer是可以将buf填充404 not found等网页内容的,不能将这个内容当成文件内容,所以需要判断http web返回来的code,进行判断。
3.我有个问题,就是想得到服务器上filename的具体名称,verbose调试已经返回了,但是我在getinfo的时候,试过好多选项,但未找到这个存放真实服务器文件名的选项,如果有知道的麻烦告诉我,谢谢了!
#include "curl/curl.h"
#pragma comment(lib, "libcurl.lib") long writer(void *data, int size, int nmemb, string &content);
bool CurlInit(CURL *&curl, const char* url,string &content);
bool GetURLDataBycurl(const char* URL, string &content); void main()
{
char *url ="http://www.baidu.com/img/baidu.gif";
string content;
if ( GetURLDataBycurl(url,content))
{
printf("%s\n",content); }
getchar();
} bool CurlInit(CURL *&curl, const char* url,string &content)
{
CURLcode code;
string error;
curl = curl_easy_init();
if (curl == NULL)
{
printf( "Failed to create CURL connection\n");
return false;
}
code = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
if (code != CURLE_OK)
{
printf( "Failed to set error buffer [%d]\n", code );
return false;
}
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
code = curl_easy_setopt(curl, CURLOPT_URL, url);
if (code != CURLE_OK)
{
printf("Failed to set URL [%s]\n", error);
return false;
}
code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, );
if (code != CURLE_OK)
{
printf( "Failed to set redirect option [%s]\n", error );
return false;
}
code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
if (code != CURLE_OK)
{
printf( "Failed to set writer [%s]\n", error);
return false;
}
code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
if (code != CURLE_OK)
{
printf( "Failed to set write data [%s]\n", error );
return false;
}
return true;
} long writer(void *data, int size, int nmemb, string &content)
{
long sizes = size * nmemb;
string temp(data,sizes);
content += temp;
return sizes;
} bool GetURLDataBycurl(const char* URL, string &content)
{
CURL *curl = NULL;
CURLcode code;
string error; code = curl_global_init(CURL_GLOBAL_DEFAULT);
if (code != CURLE_OK)
{
printf( "Failed to global init default [%d]\n", code );
return false;
} if ( !CurlInit(curl,URL,content) )
{
printf( "Failed to global init default [%d]\n" );
return PM_FALSE;
}
code = curl_easy_perform(curl);
if (code != CURLE_OK)
{
printf( "Failed to get '%s' [%s]\n", URL, error);
return false;
}
long retcode = ;
code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &retcode);
if ( (code == CURLE_OK) && retcode == )
{
double length = ;
code = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD , &length);
printf("%d",retcode);
FILE * file = fopen("1.gif","wb");
fseek(file,,SEEK_SET);
fwrite(content.c_str(),,length,file);
fclose(file); //struct curl_slist *list;
//code = curl_easy_getinfo(curl,CURLINFO_COOKIELIST,&list);
//curl_slist_free_all (list); return true;
}
else
{
// debug1( "%s \n ",getStatusCode(retcode));
return false;
}
curl_easy_cleanup(curl);
return false;
}
原地址:http://www.cppblog.com/qiujian5628/archive/2008/06/28/54873.html
libcurl使用心得-包括下载文件不存在处理相关(转)的更多相关文章
- libcurl 通过http协议下载文件并显示下载进度
vc6 测试工程下载地址:http://download.csdn.net/detail/mtour/8068053 代码如下: size_t my_write_func(void *ptr, siz ...
- libcurl开源库在Win7 + VS2012环境下编译、配置详解 以及下载文件并显示下载进度 demo(转载)
转载:http://blog.csdn.net/fengshuiyue/article/details/39530093(基本教程) 转载:https://my.oschina.net/u/14207 ...
- python网络爬虫之使用scrapy下载文件
前面介绍了ImagesPipeline用于下载图片,Scrapy还提供了FilesPipeline用与文件下载.和之前的ImagesPipeline一样,FilesPipeline使用时只需要通过it ...
- 使用libcurl下载文件小例
libcurl是一个很强大的开源网络处理库,支持包括HTTP.HTTPS.FTP……一系列网络协议.用它来进行HTTP的get\post 或者下载文件更是小菜一碟,chrome内核都用到了它,本文主要 ...
- 使用libcurl开源库和Duilib做的下载文件并显示进度条的小工具
转载:http://blog.csdn.net/mfcing/article/details/43603525 转载:http://blog.csdn.net/infoworld/article/de ...
- Spring Boot之 Controller 接收参数和返回数据总结(包括上传、下载文件)
一.接收参数(postman发送) 1.form表单 @RequestParam("name") String name 会把传递过来的Form表单中的name对应 ...
- python网络编程-socket上传下载文件(包括md5验证,大数据发送,粘包处理)
ftp server 1) 读取文件名 2)检查文件是否存在 3)打开文件 4)检查文件大小 5)发送文件大小给客户端 6)等客户端确认 7)开始边读边(md5计算)发数据 8)给客户端发md5 ft ...
- MVC下载文件方式 包括网络地址文件
MVC下载文件方式 方式一: public FileStreamResult DownFile(string filePath, string fileName){ string absol ...
- 【第十三篇】mvc下载文件,包括配置xml保护服务端文件不被外链直接访问
这里先说下载文件 <a style="color:black; margin-right:3px;" onclick="dowAtt(' + index + ')& ...
随机推荐
- 黑暗世界的搜索引擎 https://fofa.so/ https://www.shodan.io https://www.zoomeye.org 查找设备漏洞
from:http://www.freebuf.com/sectool/121339.html 什么是 Shodan? 首先,Shodan 是一个搜索引擎,但它与 Google 这种搜索网址的搜索引擎 ...
- URAL 1040 Airline Company 构造,思路 难度:2
http://acm.timus.ru/problem.aspx?space=1&num=1040 题目要求在一个联通无向图中找出一种方法给边标号使得任意一个有多条边的点,边的号码的最大公约数 ...
- IIS上部署DotNet Core程序
1.安装托管捆绑包 https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/iis/index?view=aspnetcore-2.1 ...
- 【转】 linux的网络接口之扫盲
[转] linux的网络接口之扫盲 转自:http://blog.csdn.net/zhangxinrun/article/details/6820433 (1)网络接口的命名 这里并不存在一定的命名 ...
- Jenkins用户权限管理
一.插件安装 插件:Role-based Authorization Strategy版本:2.3.2 二.全局安全配置 进入Jenkins后点击系统管理进入全局安全配置 当插件安装好的时候,授权策略 ...
- c#.net利用RNGCryptoServiceProvider产生任意范围强随机数的办法
//这样产生0 ~ 100的强随机数(含100)int max = 100;int rnd = int.MinValue;decimal _base = (decimal)long.MaxValue; ...
- JMeter VS LoadRunner
对比项 JMeter LoadRunner 安装 简单,下载解压即可 复杂,安装包大于1GB,安装时间较久 录制/回放模式 支持 支持 测试协议 偏少,但用户可自行拓展 较多,用户不可自行拓展 分布式 ...
- 在sublime中使用cppcheck
要想在sublime中使用cppcheck很简单,只需要安装两个插件就可以了:Sublimelinter 和 Sublimelinter-cppcheck 安装完成后在Sublimelinter的配置 ...
- L3-011 直捣黄龙 (30 分)
本题是一部战争大片 —— 你需要从己方大本营出发,一路攻城略地杀到敌方大本营.首先时间就是生命,所以你必须选择合适的路径,以最快的速度占领敌方大本营.当这样的路径不唯一时,要求选择可以沿途解放最多城镇 ...
- 初识安卓小程序(Android短信发送器)
首先,先创建一个安卓项目(我的版本号是4.4.2的),名字为"短信发送器" 然后在res目录下找到layout目录,找到activity_main.xml或fragment_mai ...