libcurl note(Http应用)

设置Callback function处理Http头,返回内容,进度
CURLOPT_WRITEFUNCTION
CURLOPT_WRITEDATA

CURLOPT_HEADERFUNCTION
CURLOPT_HEADERDATA

CURLOPT_NOPROGRESS
CURLOPT_PROGRESSFUNCTION
CURLOPT_PROGRESSDATA

设置连接等待时间,传输等待时间:
CURLOPT_TIMEOUT:
CURLOPT_CONNECTIONTIMEOUT:

设置重定位URL:
CURLOPT_FOLLOWLOCATION

实现断点续传:
CURLOPT_RANGE:
CURLOPT_RESUME_FROM:
注: 在我的测试中 这两个参数无效。 设置RANGE后 下载全部数据,而不是后续数据;设置RESUME_FROM后,程序无响应。


Http头设置:
Range: bytes=xx-       [可以用来实现断点续传]
User-Agent: xx
Location:              [网页重定位 url]
Set-Cookie:            [Cookie]
Content-Length:        [报文长度]
Content-Type:            [报文类型]


例程:

test()
{
        CURL *curl;
        CURLcode res;
        struct curl_slist *slist_header = NULL;
                                                                                
        FILE *pFile_error = fopen(CURL_ERROR_FILE, "w+");
                                                                                
        curl = curl_easy_init();
        if(curl)
        {
                slist_header = curl_slist_append(slist_header, version_id.data());
                curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist_header);
                                                                                
                QString follow_location=QString("With follow location");
                curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, follow_location.data());
                curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
                curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout_connect); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);

                curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);

                curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);

curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &progress_percent) ;              
                curl_easy_setopt(curl, CURLOPT_WRITEDATA, &recv_buf);
                curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback); curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header_buf);
                curl_easy_setopt(curl, CURLOPT_POST, TRUE);
                curl_easy_setopt(curl, CURLOPT_POSTFIELDS,zip_buf);
                curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,zip_len);
                curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
                curl_easy_setopt(curl, CURLOPT_STDERR, pFile_error);
                int res = curl_easy_perform(curl);
                if (res == 0) {
                        .......
                }
                curl_easy_cleanup(curl);
                curl_slist_free_all(slist_header);
                fflush(pFile_error);
                fclose(pFile_error);
        }
}

 

 

CURLOPT_RESUME_FROM参数实验有效的。我使用的时候,他当前文件的大小作为参数一起传递进去,他会传递后续数据。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值