赞助

libcurl进行HTTP GET获取JSON数据(转载)

转载:http://blog.csdn.net/vincent2610/article/details/68488365

#include <stdio.h>
#include <iostream>
#include <sstream>
using namespace std;

size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) 
{
    string data((const char*) ptr, (size_t) size * nmemb);

    *((stringstream*) stream) << data << endl;

    return size * nmemb;
}

int main(int argc, char *argv[])
{
    
        // 基于当前系统的当前日期/时间
        time_t now = time(0);
        char* dt = ctime(&now);
        cout << dt << "-------------------------------------" << endl;

        /*HTTP GET json data*/
        std::stringstream out;
        void* curl = curl_easy_init();
        // 设置URL
        curl_easy_setopt(curl, CURLOPT_URL, "http://if.qdocument.net:705/bic/download/initFiles?uid=weidong0925@126.com&pageNum=1");
        // 设置接收数据的处理函数和存放变量
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out);

        // 执行HTTP GET操作
        CURLcode res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        }

        // 接受数据存放在out中,输出之
        //cout << out.str() << endl;
        string str_json = out.str();


        printf("%s",str_json.c_str());
        curl_easy_cleanup(curl);

    return 0;
}

 

posted @ 2017-07-26 12:00  车臣  阅读(10591)  评论(0编辑  收藏  举报