libcurl programming

15 篇文章 0 订阅

Compiling

On windows platform, goto the unpack folder, such as d:/libcurl/curl, find the winbuild diretory. Open the vs command line window and use “nmake makefile.vc” to compile the code, here is the sample to compile the libcurl library to x86 debug static library.

nmake makefile.vc vc=10 mode=static machine=x86 debug=yes

Linking

If you want to link the static library of libcurl to your project, you need to define the macro “CURL_STATICLIB” before including relate header, such as “<curl/curl.h>”, “<curl/easy.h>”.
#define CURL_STATICLIB
#include <curl/curl.h>
 

Macro

LIBCURL_VERSION_NUM: version for libcurl, value “0x070c03” means version 7.12.3
CURL_STATICLIB: using static library

With C++

There's basically only one thing to keep in mind when using C++ instead of C when interfacing libcurl: 
The callbacks  CANNOT be non-static class member functions .
 

API

There are three category for libcurl, easy, mult and share.
#include <curl/easy.h>
#include <curl/multi.h>

Easy interafce

curl_easy_init

This function must be the first function to call, and it returns a CURL easy handle that you must use as input to other easy-functions. curl_easy_init contains curl_global_init invoking automatically if you have not yet called it. But this is lethal in multi-threaded cases, since curl_global_init is not thread-safe.

You are strongly advised to not allow this automatic behaviour, by calling curl_global_init yourself properly.

If it returns NULL to curl pointer, something went wrong and you can not use the other funcitons.

curl_easy_cleanup

It is oppsoite of the curl_easy_init function and it should be the last function call for an easy sesstion. Any uses of the handlel after this function has been called and hanve returned, are illegal. This function return None.

curl_easy_escape
URL encoding. For the character in the url string inputed as parameter, that is not a-z, A-Z, 0-9, '-', '.', '_' or '~' are converted to their "URL escaped" version (%NN where NN is a two-digit hexadecimal number). Here is sample code for it. 
CURL * curl = nullptr;
char * esc = nullptr;
CURLcode ret;
ret = curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
esc = curl_easy_escape(curl, "http://lcoalhost", 0);
cout<<esc<<endl;
curl_free((void*)esc);
curl_easy_cleanup(curl);
Output is: http%3A%2F%2Flcoalhost

curl_easy_getinfo

curl_easy_setopt
CURLOPT_URL 
Pass in a pointer to the actual URL to deal with. The parameter should be a char * to a zero terminated string which must be URL-encoded in the following format:
scheme://host:port/path
 

Practise

Using C++ non-static functions for callbacks?

libcurl is a C library, it doesn't know anything about C++ member functions. 
You can overcome this "limitation" with a relative ease using a static member function that is passed a pointer to the class:

// f is the pointer to your object.
static YourClass::func(void *buffer, size_t sz, size_t n, void *f)
{
  // Call non-static member function.
  static_cast<YourClass*>(f)->nonStaticFunction();
}

// This is how you pass pointer to the static function:
curl_easy_setopt(hcurl, CURLOPT_WRITEFUNCTION, YourClass::func);
curl_easy_setopt(hcurl, CURLOPT_WRITEDATA, this);

Get the remote file’s size

option setting
curl_easy_setopt(handle, CURLOPT_URL, url);
curl_easy_setopt(handle, CURLOPT_PROXY, szProxy); // for proxy
curl_easy_setopt(handle, CURLOPT_NOBODY, 1);
curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, szErr);
ret = curl_easy_perform(handle);
get information
double dval;
if (CURLE_OK != curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &dval))
{
    cerr<<"Failed to get CURLINFO_CONTENT_LENGTH_DOWNLOAD"<<endl;
    curl_easy_cleanup(curl);
    return;
}
cout<<"CURLINFO_CONTENT_LENGTH_DOWNLOAD :"<<dval<<endl;

Get the remote file’s create time

option setting
curl_easy_setopt(handle, CURLOPT_FILETIME, 1L); // for curl_easy_getinfo() for CURLINFO_FILETIME
get information
long lval;
if (CURLE_OK != curl_easy_getinfo(curl, CURLINFO_FILETIME, &lval) || lval == -1)
{
    cerr<<"Failed to get CURLINFO_FILETIME"<<endl;
    curl_easy_cleanup(curl);
    return;
}
time_t t(lval);
tm * ptm = localtime((const time_t*)&t);
cout<<"CURLINFO_FILETIME :"<<asctime(ptm)<<endl;

Show progress for downloading

option setting using default display by cURL, 0L means showing progress, 1L means not to show it.
curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L);
change to use 
 
Check/Confirm the remote file exist
Two flags could show whether the remote file exists by the url,  CURLINFO_RESPONSE_CODE and CURLINFO_CONTENT_LENGTH_DOWNLOAD
If the file doesn’t exist by the url, 404 will be set as  CURLINFO_RESPONSE_CODE and –1 for  CURLINFO_CONTENT_LENGTH_DOWNLOAD. Otherwise, 200 for  CURLINFO_RESPONSE_CODE.
 
Resume uploading file
the option  CURLOPT_RESUME_FROM by curl_easy_setopt
CURLOPT_RESUME_FROM_LARGE
Pass a curl_off_t as parameter. It contains the offset in number of bytes that you want the transfer to start from. (Added in 7.11.0)
 

AGet is similar to FlashGet for win32. It can download the large file by multithread. Here is the theory for aget:
If you're downloading a file of size less than 512K, I suggest using small segments, i.e 4-5 however, if you are downloading large files, you can increase the segmentation Aget first sends a HEAD request to retrieve the length of the file, and divides it into equal segments according to the number user has requested. Then for each segment, it connects to the server and gets only the part, which it is to download. Therefore, if you're downloading a small file, it is suggested that you decrease the number of segmentation, likewise, if it's a large file, you're urged to increase the number of threads.
 

Todo
using libcurl as spider and 爬虫
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值