1. 程式人生 > >使用libcurl傳送HTTP請求的一個簡單示例程式碼

使用libcurl傳送HTTP請求的一個簡單示例程式碼

程式碼簡單解釋

設定header

首先要宣告header的結構體變數,然後設定對應header值,最後將其設定到curl結構體中

//宣告
CURL *curl;
struct curl_slist *headers = NULL;

//賦值header值
headers = curl_slist_append(headers, "Host: 0xz.sz.qcloud.com");
headers = curl_slist_append(headers, "yousa3: zym");

//設定header
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

指定url

//宣告url
string url = "http://10.54.69.29:80/test/usr/loveqiqian";

//指定url
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

//傳送http請求
res = curl_easy_perform(curl);

設定HTTP請求的body

這裡傳送的請求是POST請求,通過如下程式碼設定的是POST請求
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");

如果沒有指定contenttype,那麼contenttype預設是post的標準表單格式,application/x-www-form-urlencoded

設定body是如下程式碼:

//先用char[] 準備好要傳送的body字串
char hello[12] = "a=hello";

//設定body長度以及內容
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(hello));
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, hello);

//然後就可以使用easy_perform介面傳送了。

獲取響應響應碼

res獲取到的就是HTTP響應碼

res = curl_easy_perform(curl);

獲取響應header以及響應body

//宣告
string header;
string result;

//設定
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, &write_data);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);

//write_data函式是這樣
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp){
    char *d = (char*)buffer;
    string *b = (string*)(userp);
    int result = 0;
    if (b != NULL){
        b->append(d, size*nmemb);
        result = size*nmemb;
    }
    return result;
}

PS:HEAD請求的響應是沒有body的,需要額外設定一行

curl_easy_setopt(curl, CURLOPT_NOBODY, 1);

程式碼

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

using namespace std;

size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);

int main() {
    cout<<"hello world" << endl;

    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();
    string url = "http://10.54.69.29:80/test/usr/loveqiqian";
    string header;
    string result;
    struct curl_slist *headers = NULL;
    char hello[12] = "a=hello";



    headers = curl_slist_append(headers, "Host: 0xz.sz.qcloud.com");
    headers = curl_slist_append(headers, "yousa3: zym");

    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(hello));
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, hello);

    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, &write_data);
    curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
    res = curl_easy_perform(curl);

    cout << header << endl << endl;
    cout << result << endl;
    cout << res << endl;
    curl_easy_cleanup(curl);

    return 0;

}

size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp){
    char *d = (char*)buffer;
    string *b = (string*)(userp);
    int result = 0;
    if (b != NULL){
        b->append(d, size*nmemb);
        result = size*nmemb;
    }
    return result;
}