基於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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM