libcurl 是一個使用比較廣泛,并且使用很方便的http請求庫。 對于http post請求,根據head 部分的Content-Type 的不同,處理方式也不同。
對應請求數據包:
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);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);這種情況主要是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);這種情況是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);以上內容為本人在工作中遇到的情況,寫在這里做個總結,同時也給有需要的同學做個參考。
新聞熱點
疑難解答