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);以上内容为本人在工作中遇到的情况,写在这里做个总结,同时也给有需要的同学做个参考。
新闻热点
疑难解答