Libcurl为一个免费开源的,客户端url传输库,支持FTP,FTPS,TFTP,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE和LDAP,跨平台,支持Windows,Unix,Linux等,线程安全,支持Ipv6。并且易于使用。
http://curl.haxx.se/libcurl/
从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"
#pragmacomment(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);
voidmain()
{
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)
{
CURLcodecode;
string error;
curl=curl_easy_init();
if(curl==NULL)
{
printf("FailedtocreateCURLconnection\n");
return false;
}
code=curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,error);
if(code!=CURLE_OK)
{
printf("Failedtoseterrorbuffer[%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("FailedtosetURL[%s]\n",error);
return false;
}
code=curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1);
if(code!=CURLE_OK)
{
printf("Failedtosetredirectoption[%s]\n",error);
return false;
}
code=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writer);
if(code!=CURLE_OK)
{
printf("Failedtosetwriter[%s]\n",error);
return false;
}
code=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&content);
if(code!=CURLE_OK)
{
printf("Failedtosetwritedata[%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;
returnsizes;
}
bool GetURLDataBycurl(const char* URL, string
&content)
{
CURL*curl=NULL;
CURLcodecode;
string error;
code=curl_global_init(CURL_GLOBAL_DEFAULT);
if(code!=CURLE_OK)
{
printf("Failedtoglobalinitdefault[%d]\n",code);
return false;
}
if(!CurlInit(curl,URL,content))
{
printf("Failedtoglobalinitdefault[%d]\n");
returnPM_FALSE;
}
code=curl_easy_perform(curl);
if(code!=CURLE_OK)
{
printf("Failedtoget'%s'[%s]\n",URL,error);
return false;
}
long retcode=0;
code=curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE,&retcode);
if((code==CURLE_OK)&&retcode==200)
{
double length=0;
code=curl_easy_getinfo(curl,CURLINFO_CONTENT_LENGTH_DOWNLOAD,&length);
printf("%d",retcode);
FILE*file=fopen("1.gif","wb");
fseek(file,0,SEEK_SET);
fwrite(content.c_str(),1,length,file);
fclose(file);
//structcurl_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;
}
分享到:
相关推荐
libcurl使用Https说明,使用此文档里面有资源。帮助开发基于SOCKET的HTTPS传输协议。(描述可能不详)
libcurl使用demo,根据一个网址,获取它的http头信息,然后解析其中的属性值
libcurl使用demo,获取http头文件信息
libcurl库开发手册,有利于初入libcurl库开发人员进行网络上传下载的开发环境的搭建,快速入手的基础篇
libcurl 使用vs2010编译的动态库和静态库(包含的debug和release版和头文件)
iOS开发 libcurl库的简单使用
libcurl实现和网络文件系统进行通信时,当用easy模式时是阻塞的, 所以需要设置一个超时参数
libcurl URL库,网络编程
个人编译的 libcurl静态库,并且附带使用例子!
c++使用libcurl上传和下载资源,封装了库,带lib文件,带nodejs测试服务端
个人编译的 libcurl静态库,并且附带使用例子,改下IP地址就能用
用form-data 表单形式发送数据,并且上传文件,完整示例,使用libcurl提供的API上传文件。项目使用Vc++ 6.0 和 VS 2017同时编译通过,上传表单数据和文件测试成功。
第二部分是基于原生libcurl封装得到的方便用户使用的EasyCurl类的源文件(easycurl.cpp/easycurl.h),该源文件经过经过业务考验是相对成熟可靠的。在使用方面屏蔽原生libcurl的诸多内部机制,使用者关注的只是HttpGet...
libcurl
最近遇到项目,对数据传输的安全性要求不高,需要C++用post方法调用ASP.NET Web Service,开始有人推荐用gsoap,后来自己网上搜索发现用libcurl库完全够用,由于我是windows下开发,所以花功夫编译了windows版的...
lib curl 实现http client示例
这个小程序只是简单的使用线程池实现了libcurl的异步请求功能,很多地方还需要优化,之所有厚颜上传到这里,主要有有三个目的。 一个是回报这些天在CSDN上得到的帮助,希望可以帮助到后来的人; 二是感于libcurl编译...
libcurl库的安装和使用111
libcurl是一个跨平台的开源网络协议库,支持http, https, rtsp等多种协议 。libcurl同样支持HTTPS证书授权,HTTP POST, HTTP PUT, FTP 上传, HTTP基本表单上传,代理,cookies,和用户认证。