Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4733238
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2010-05-03 22:32:31

设置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);
        }
}


阅读(2028) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~