文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>libcurl note(Http应用)

libcurl note(Http应用)

时间:2010-05-03  来源:ubuntuer

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


相关阅读 更多 +
排行榜 更多 +
崩溃大陆2鱼竿如何获取

崩溃大陆2鱼竿如何获取

冒险解谜 下载
狙击手行动

狙击手行动

冒险解谜 下载
狙击突围行动最新版

狙击突围行动最新版

冒险解谜 下载