libcurl用法笔记


#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/time.h>
#include <curl/curl.h>
#include <unistd.h>
#include <string>

//static const char *gUrl = "http://127.0.0.1:8085/database/0.1/junctions?position=1&range=1&type=connection";
//static const char *gUrl = "http://127.0.0.1:8086/database/1/lanes?position=9.82178,52.2949,0&range=675";
//static const char *gUrl = "http://127.0.0.1:8086/database/status";
//static const char *gUrl = "http://127.0.0.1:8086/database/1/lanes?position=9.83204,52.300292,0&range=50";
static const char *gUrl = "http://127.0.0.1:8086/database/1/lanes?position=0,52.300292,0&range=0&onlyCheckExist=1";
static int sum = 0;

static size_t header_callback(char* buffer, size_t size, size_t nmemb, std::string* header)
{
    (*header).append(buffer, size*nmemb);
    return size*nmemb;
}
static size_t write_callback(char* ptr, size_t size, size_t nmemb, std::string* buffer)
{
    (*buffer).append(ptr, size*nmemb);
    return size*nmemb;
}

uint64_t getTimeMs(void)
{
    timeval tv;
    gettimeofday(&tv,NULL);
    uint64_t time_cur_sec = tv.tv_sec;
    uint64_t time_cur = time_cur_sec * 1000 + tv.tv_usec / 1000;
    return time_cur;
}

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        printf("usage: http_client CycleNum\n");
        return 0;
    }
    int num = atoi(argv[1]);
    int count = 0;
    uint64_t Stime = 0, Etime = 0;
    std::string header;
    std::string body;

    CURLcode res;
    CURL *curl;

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, gUrl);
        curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
        curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
        #if 1
        /* enable TCP keep-alive for this transfer */
        curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
 
        /* keep-alive idle time to 120 seconds */
        curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 120L);
 
        /* interval time between keep-alive probes: 60 seconds */
        curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 60L);
        #endif
        while(count < num)
        {
            header = "";
            body = "";
            //Stime = getTimeMs();
            res = curl_easy_perform(curl);
            if(CURLE_OK == res) {
            } else {
                printf("Error: invalid URL: %s, curl_easy_perform : %d\n", gUrl, res);
            }
            //Etime = getTimeMs();
            count++;
            //sum += (Etime-Stime);
            //printf("[client]Count: %d, Stime: %llu, Etime: %llu, interval: %llu, sum: %d\n", count, Stime, Etime, Etime-Stime, sum);
            //printf("get header: %s\nbody: %s\n", header.c_str(), body.c_str());
           printf("get header: %s\n", header.c_str());
           //printf("\n%s\n", body.c_str());
           //sleep(10);
        }
        curl_easy_cleanup(curl);
    }
    return 0;
}













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值