当前位置: 首页>>代码示例>>C++>>正文


C++ Downloader::setHeaders方法代码示例

本文整理汇总了C++中Downloader::setHeaders方法的典型用法代码示例。如果您正苦于以下问题:C++ Downloader::setHeaders方法的具体用法?C++ Downloader::setHeaders怎么用?C++ Downloader::setHeaders使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Downloader的用法示例。


在下文中一共展示了Downloader::setHeaders方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Request

void Http::Request(const string &url, bool async, bool usePost, const void *data, size_t dataLen, HttpCallback callback,
                   void *callbackArg, map<std::string, string> *header, int timeout)
{
    if( timeout <= 0 ) {
        timeout = 60;
    }
    
    CURL *handle = curl_easy_init();
    Downloader *downloader = new Downloader(handle, callback, callbackArg);
    
    curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
    curl_easy_setopt(handle, CURLOPT_TIMEOUT, timeout);
    curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, writer);
    curl_easy_setopt(handle, CURLOPT_FORBID_REUSE, 1);
    curl_easy_setopt(handle, CURLOPT_WRITEDATA, downloader);
    curl_easy_setopt(handle, CURLOPT_PRIVATE, downloader);
    if( usePost ){
        curl_easy_setopt(handle, CURLOPT_POST, 1);
        curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, dataLen);
        curl_easy_setopt(handle, CURLOPT_COPYPOSTFIELDS, data);
    }
    if( header != NULL ) {
        struct curl_slist *headers = NULL;
        map<string,string>::const_iterator it = header->begin();
        for(; it != header->end(); it++ ) {
            headers = curl_slist_append(headers, (it->first + ": " + it->second).c_str());
        }
        curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
        downloader->setHeaders(headers);
    }
    
    if( async ) {
        curl_multi_add_handle(m_mcurl, handle);
        int running = 0;
        curl_multi_perform(m_mcurl, &running);
    }else {
        int repCode = -1;
        CURLcode code = curl_easy_perform(handle);
        if ( CURLE_OK == code )
        {
            curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &repCode);
        }
        
        downloader->didFinish(code, repCode);
        delete  downloader;
    }
}
开发者ID:renbing,项目名称:Giraffe,代码行数:47,代码来源:Http.cpp

示例2: Get

int Http::Get(const string &url, string &content, map<std::string, string> *header, int timeout)
{
    if( timeout <= 0 ) {
        timeout = 60;
    }
    
    CURL *handle = curl_easy_init();
    Downloader *downloader = new Downloader(handle);
    
    curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
    curl_easy_setopt(handle, CURLOPT_TIMEOUT, timeout);
    curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, writer);
    curl_easy_setopt(handle, CURLOPT_FORBID_REUSE, 1);
    curl_easy_setopt(handle, CURLOPT_WRITEDATA, downloader);
    curl_easy_setopt(handle, CURLOPT_PRIVATE, downloader);
    if( header != NULL ) {
        struct curl_slist *headers = NULL;
        map<string,string>::const_iterator it = header->begin();
        for(; it != header->end(); it++ ) {
            headers = curl_slist_append(headers, (it->first + ": " + it->second).c_str());
        }
        curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
        downloader->setHeaders(headers);
    }
    
    int repCode = -1;
    CURLcode code = curl_easy_perform(handle);
    if ( CURLE_OK == code )
    {
        curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &repCode);
    }
    
    int downloaded = -1;
    
    if( repCode == 200 ) {
        content = downloader->getText();
        downloaded = content.size();
    }
    
    delete  downloader;
    
    return downloaded;
}
开发者ID:renbing,项目名称:Giraffe,代码行数:43,代码来源:Http.cpp


注:本文中的Downloader::setHeaders方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。