libcurl请求示例

1.程序代码

#include 
#include 
#include 
#include 

struct mem_save_struct
{
        char *memory;
        size_t size;
};

size_t write_memory_callback(void *ptr, size_t size, size_t nmemb, void *data)
{
        struct mem_save_struct *mem = (struct mem_save_struct *)data;
        size_t realsize = size * nmemb;

        mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
        if(mem->memory)
        {
                memcpy(&(mem->memory[mem->size]), ptr, realsize);
                mem->size += realsize;
                mem->memory[mem->size] = 0;
        }

        return realsize;
}

int main()
{
        struct mem_save_struct oDataChunk;                                              //请求结果的保存格式
        struct curl_slist *pList = NULL;
        long res_code = 0;
        char *url = "http://172.16.79.128:15672/";
        char *filename = "index.html";

        CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
        if(CURLE_OK != res)
        {
                printf("curl init failed\n");
                return 1;
        }

        CURL *pCurl = curl_easy_init();
        if(NULL == pCurl)
        {
                printf("Init CURL failed...\n");
                curl_global_cleanup();
                return -1;
        }

        oDataChunk.memory = (char *)malloc(1);
        oDataChunk.memory = 0x00;
        oDataChunk.size = 0;

        curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 3L);                           //请求超时时长
        curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, 10L);                   //连接超时时长
        curl_easy_setopt(pCurl, CURLOPT_FOLLOWLOCATION, 1L);                    //允许重定向
        curl_easy_setopt(pCurl, CURLOPT_HEADER, 0L);                            //若启用,会将头文件的信息作为数据流输出
        curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, write_memory_callback);  //得到请求结果后的回调函数

        curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &oDataChunk);                //请求结果保存
        curl_easy_setopt(pCurl, CURLOPT_NOSIGNAL, 1L);                          //关闭中断信号响应
        curl_easy_setopt(pCurl, CURLOPT_VERBOSE, 1L);                           //启用时会汇报所有的信息
        curl_easy_setopt(pCurl, CURLOPT_URL, url);                              //需要获取的URL地址

        pList = curl_slist_append(pList,"Accept-Encoding:gzip, deflate, sdch");
        pList = curl_slist_append(pList,"Accept-Language:zh-CN,zh;q=0.8");
        pList = curl_slist_append(pList,"Connection:keep-alive");
        curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, pList);

        res = curl_easy_perform(pCurl);                                         //执行请求
        res = curl_easy_getinfo(pCurl, CURLINFO_RESPONSE_CODE, &res_code);

        //正确响应后,请求转写成本地文件的文件
        if((res == CURLE_OK) && (res_code == 200 || res_code == 201))
        {
                char *fTmpMem = oDataChunk.memory;
                if(!fTmpMem)
                {
                        curl_slist_free_all(pList);
                        curl_easy_cleanup(pCurl);
                        curl_global_cleanup();
                        return -1;
                }

                FILE *fp = fopen(filename, "wb");
                if(!fp)
                {
                        free(fTmpMem);
                        printf("open file failed\n");
                        curl_slist_free_all(pList);
                        curl_easy_cleanup(pCurl);
                        curl_global_cleanup();
                        return -1;
                }

                fwrite(fTmpMem, 1, oDataChunk.size, fp);

                //printf("hello[%d]\n", __LINE__);

                free(fTmpMem);

                fclose(fp);

                curl_slist_free_all(pList);
                curl_easy_cleanup(pCurl);
                curl_global_cleanup();

                return 0;
        }

        //printf("hello[%d]\n", __LINE__);

        curl_slist_free_all(pList);
        curl_easy_cleanup(pCurl);
        curl_global_cleanup();

        return 0;
}

2.编译源码

$ gcc -o curl-test curl_test.c -I$HOME/local/include -lcurl

3.运行及结果

$ ./curl-test
* About to connect() to 172.16.79.128 port 15672 (#0)
*   Trying 172.16.79.128... * connected
* Connected to 172.16.79.128 (172.16.79.128) port 15672 (#0)
> GET / HTTP/1.1
Host: 172.16.79.128:15672
Accept: */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:zh-CN,zh;q=0.8
Connection:keep-alive

< HTTP/1.1 200 OK
< server: Cowboy
< date: Wed, 06 Nov 2019 23:30:20 GMT
< content-length: 1419
< content-type: text/html
< etag: "1217749438"
< last-modified: Wed, 06 Nov 2019 22:06:50 GMT
< 
* Connection #0 to host 172.16.79.128 left intact
* Closing connection #0

你可能感兴趣的:(libcurl请求示例)