基于libcurl的GET与POST(HTTP1.1)

Posted liuzhenbo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于libcurl的GET与POST(HTTP1.1)相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <curl/curl.h>

bool getUrl(char *filename)

    CURL *curl;
    CURLcode res;                 
    FILE *fp;
    if ((fp = fopen(filename, "w")) == NULL)  // 返回结果用文件存储
        return false;
    struct curl_slist *headers = NULL;
   // headers = curl_slist_append(headers, "Accept: Agent-007");
    curl = curl_easy_init();    // 初始化
    if (curl)
    
        //curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");// 代理
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 改协议头
        curl_easy_setopt(curl, CURLOPT_URL,"http://www.nengyouyun.cn/user/getAppversionnew2?apptype=H5C899DDC");
  // curl_easy_setopt(curl, CURLOPT_URL,"http://localhost/");
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); //将返回的html主体数据输出到fp指向的文件
        curl_easy_setopt(curl, CURLOPT_HEADERDATA, fp); //将返回的http头输出到fp指向的文件
        res = curl_easy_perform(curl);   // 执行
        if (res != 0) 

            curl_slist_free_all(headers);
            curl_easy_cleanup(curl);
        
        fclose(fp);
        return true;
    

bool postUrl(char *filename)

    CURL *curl;
    CURLcode res;
    FILE *fp;
    
    struct curl_httppost *formpost = NULL;
    struct curl_httppost *lastptr = NULL;    

    if ((fp = fopen(filename, "w")) == NULL)
        return false;
    curl = curl_easy_init();
    if (curl)
    
       // curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie.txt"); // 指定cookie文件

        curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "image",CURLFORM_COPYCONTENTS,"1.jpg",CURLFORM_END);
       //  curl_easy_setopt(curl,CURLOPT_HTTPPOST,formpost);     // POST  -liuzhenbo
        //curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "&logintype=uid&u=xieyan&psw=xxx86");    // 指定post内容
        //curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");
        curl_easy_setopt(curl, CURLOPT_URL, "http://www.nengyouyun.cn/user/getAppversionnew2?apptype=H5C899DDC");   // 指定url
        curl_easy_setopt(curl,CURLOPT_HTTPPOST,formpost);     // POST  -liuzhenbo
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);  //HTTP主体数据 -liuzhenbo
        curl_easy_setopt(curl, CURLOPT_HEADERDATA, fp); //将返回的http头输出到fp指向的文件
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        curl_formfree(formpost);
    
    fclose(fp);
    return true;

int main(void)

    getUrl("get");
    postUrl("post");

GET方式接收到服务器端发来的http头:

HTTP/1.1 200 
Server: nginx/1.10.0 (Ubuntu)
Date: Mon, 17 Jun 2019 11:34:45 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Application-Context: cloud-gateway:7001

POST方式接收到服务器发来的http头:

HTTP/1.1 100 Continue

HTTP/1.1 200 
Server: nginx/1.10.0 (Ubuntu)
Date: Mon, 17 Jun 2019 11:34:45 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Application-Context: cloud-gateway:7001

注:使用HTTP/1.1协议的curl,当要POST的数据大于1024字节的时候, curl并不会直接就发起POST请求, 而是会分为俩步。(我认为主要是为了节省资源)

 流程如下:

     (1)发送一个请求, 包含一个Expect:100-continue, 询问Server使用愿意接受数据

     (2)接收到Server返回的100-continue应答以后, 把数据POST给Server

以上是关于基于libcurl的GET与POST(HTTP1.1)的主要内容,如果未能解决你的问题,请参考以下文章

libcurl get和post的区别

HttpClient使用详解与实战一:普通的GET和POST请求

HttpClient使用详解与实战一:普通的GET和POST请求

HTTP 请求

如何使用SOCKET 发送HTTP1.1 GET POST请求包

基于libcurl的POST(http)