C語言 HTTP傳送post和get請求

2021-10-11 13:53:40 字數 1412 閱讀 2783

安裝curl環境:

apt install curl

apt-get install libcurl4-openssl-dev

使用c語言來做http協議,然後傳送post和get請求,這裡為post請求的**,如下:

#include #include #include #include #include "myjson.h"

#define posturl "127.0.0.1:8080/viid/system/register"

#define postfields ""

int main(int argc,char *ar**)

curl_easy_setopt(curl,curlopt_url,posturl); //url位址

curl_easy_setopt(curl, curlopt_ssl_verifypeer, 0); //不檢查ssl,可訪問https

curl_easy_setopt(curl, curlopt_ssl_verifyhost, 0); //不檢查ssl,可訪問https

curl_easy_setopt(curl,curlopt_postfields,postfields); //post引數

curl_easy_setopt(curl,curlopt_post,1); //設定問非0表示本次操作為post

curl_easy_setopt(curl,curlopt_verbose,1); //列印除錯資訊

curl_easy_setopt(curl,curlopt_followlocation,1); //設定為非0,響應頭資訊location

curl_easy_setopt(curl, curlopt_connecttimeout, 10);

curl_easy_setopt(curl, curlopt_timeout, 10);//接收資料時超時設定,如果10秒內資料未

res = curl_easy_perform(curl);

if (res != curle_ok)

return -1; }

curl_easy_cleanup(curl);

printf("\n");

return 0;

}

若需要傳送get請求,只需注釋如下**即可,預設為get的,如下:

curl_easy_setopt(curl,curlopt_postfields,postfields); //post引數

curl_easy_setopt(curl,curlopt_post,1); //設定問非0表示本次操作為post

參考部落格:

HTTP傳送POST請求說明

簡訊下發工具類介面 邏輯與技術要點概括 開發工具 myeclipse9.0 tomcat6.0x 測試方式 main方法體現,直接呼叫當前類內部介面方法 引數都走配置化 工具類主方法 sendmsgupdown 方法體體現內容 1 引數配置化 2 引數值獲取與校驗 eq string url par...

C 模擬http 傳送post或get請求

模擬http提交表單並獲取返回資料 post 提交位址 引數 模擬http提交表單並獲取返回資料 get 提交位址 引數 在post的時候有時也用的到cookie,像登入163發郵件時候就需要傳送cookie,所以在外部乙個cookie屬性隨時儲存 cookiecontainer cookie ne...

C 模擬http 傳送post或get請求

在post的時候有時也用的到cookie,像登入163發郵件時候就需要傳送cookie,所以在外部乙個cookie屬性隨時儲存 cookiecontainer cookie new cookiecontainer 注意 有時候請求會重定向,但我們就需要從重定向url獲取東西,像qq登入成功後獲取si...