国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

libcurl post 請求

2019-11-08 02:01:34
字體:
來源:轉載
供稿:網友

libcurl 是一個使用比較廣泛,并且使用很方便的http請求庫。 對于http post請求,根據head 部分的Content-Type 的不同,處理方式也不同。

1. 普通post請求

一般的post請求格式: Content-Type: application/x-www-form-urlencoded

對應請求數據包:

POST /test.php HTTP/1.1Accept: */*Accept-Language: zh-cnReferer: http://www.host.comContent-Type: application/x-www-form-urlencodedHost: www.host.comContent-Length: 66Connection: Keep-Aliveop=op1&op2=op2

對應的代碼:

CURL *curl= curl_easy_init(); if (NULL==curl) { //do something } string strUrl="www.host.com/test.php"; string postret; string strPostData="op=op1&op2=op2"; curl_easy_setopt(curl,CURLOPT_URL,strUrl.c_str()); curl_easy_setopt(curl,CURLOPT_POST, 1); curl_easy_setopt(curl,CURLOPT_WRITEDATA,&postret); curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, curlWriteCallback); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPostData.c_str()); curl_easy_perform(curl);

2. 復雜post請求

Content-Type: multipart/form-data; boundary=————————6bc6e0b955199aa1 其中boundary 為隨機生成。 對應請求數據包:

POST /sample.php HTTP/1.1Host: www.test.cnAccept: */*Content-Length: 838Expect: 100-continueContent-Type: multipart/form-data; boundary=------------------------6bc6e0b955199aa1--------------------------6bc6e0b955199aa1Content-Disposition: form-data; name="name1"value1--------------------------6bc6e0b955199aa1Content-Disposition: form-data; name="name2"value2--------------------------6bc6e0b955199aa1Content-Disposition: form-data; name="name3"value3--------------------------6bc6e0b955199aa1Content-Disposition: form-data; name="name4"value4--------------------------6bc6e0b955199aa1--

對應代碼:

CURL *curl= curl_easy_init(); if (NULL==curl) { //do something } curl_httppost *post=NULL; curl_httppost *last=NULL; string url = "www.test.cn/sample.php"; curl_easy_setopt(curl,CURLOPT_URL,url.c_str()); curl_easy_setopt(curl,CURLOPT_POST, 1); curl_formadd(&post, &last, CURLFORM_COPYNAME,"name1", CURLFORM_COPYCONTENTS,"value1", CURLFORM_END); curl_formadd(&post, &last, CURLFORM_COPYNAME,"name2", CURLFORM_COPYCONTENTS,"value2", CURLFORM_END); curl_formadd(&post, &last, CURLFORM_COPYNAME,"name3", CURLFORM_COPYCONTENTS,"value3", CURLFORM_END); curl_formadd(&post, &last, CURLFORM_COPYNAME,"name4", CURLFORM_COPYCONTENTS,"value4", CURLFORM_END); curl_easy_setopt(curl,CURLOPT_WRITEDATA,&postret); curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, curlWriteCallback); curl_easy_setopt(curl,CURLOPT_HTTPPOST, post); curl_easy_perform(curl);

3. 特殊post請求

這種情況主要是Content-Type 有特殊要求。 請求數據包:

POST /test.php HTTP/1.1Host: www.test.comAccept: */*Content-Type:application/json;charset=UTF-8Content-Length: 24{"key":"value"}

此post請求中就要求post數據部分必須為json格式。 對應代碼:

CURL *curl= curl_easy_init(); if (NULL==curl) { //do something } string url = "www.test.com/test.php"; string strPostData="{/"key/":/"value/"}"; curl_easy_setopt(curl,CURLOPT_URL,url.c_str()); curl_easy_setopt(curl,CURLOPT_POST, 1); curl_slist *plist = curl_slist_append(NULL, "Content-Type:application/json;charset=UTF-8"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, plist); curl_easy_setopt(curl,CURLOPT_WRITEDATA,&postret); curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, curlWriteCallback); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPostData.c_str()); curl_easy_perform(curl);

4. 特殊Get請求

這種情況是Get請求帶參數,但是參數不在URL中,參數是在content部分,有點像post請求,但是確實不是post請求。 對應數據包:

GET /test.php HTTP/1.1Host: www.test.comAccept: */*Content-Length: 100Content-Type: application/x-www-form-urlencoded{"query": {"type":"type1","key": {"key1": ["value1"]}}}

對應代碼:

CURL *curl= curl_easy_init(); if (NULL==curl) { //do something } string url = "www.test.com/test.php"; string strPostData="{/"query/": {/"type/":/"type1/",/"key/": {/"key1/": [/"value1/"]}}}"; curl_easy_setopt(curl,CURLOPT_URL,url.c_str()); curl_easy_setopt(curl,CURLOPT_WRITEDATA,&postret); curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, curlWriteCallback); curl_easy_setopt(curl,CURLOPT_POSTFIELDS, strPostData.c_str()); curl_easy_setopt(curl,CURLOPT_CUSTOMREQUEST, "GET"); curl_easy_perform(curl);

以上內容為本人在工作中遇到的情況,寫在這里做個總結,同時也給有需要的同學做個參考。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 衡东县| 福贡县| 汝城县| 满城县| 宁夏| 六盘水市| 洪江市| 临清市| 容城县| 兴山县| 开化县| 泽库县| 齐齐哈尔市| 仙游县| 渝北区| 乐山市| 托克逊县| 南城县| 商城县| 滨州市| 清徐县| 海安县| 固阳县| 军事| 苍溪县| 东乌珠穆沁旗| 汤阴县| 大名县| 绥阳县| 滁州市| 武山县| 分宜县| 巧家县| 巴彦淖尔市| 博野县| 囊谦县| 来凤县| 龙陵县| 郸城县| 山丹县| 高雄县|