溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

libcurl第七課 multipart/formdata表單使用

發(fā)布時間:2020-09-12 12:14:24 來源:網絡 閱讀:1541 作者:fengyuzaitu 欄目:軟件技術

場景
???????? multipart/form-data是瀏覽器用表單上傳文件的方式。最常見的情境是:在寫郵件時,向郵件后添加附件,附件通常使用表單添加,也就是用multipart/form-data格式上傳到服務器。Http服務器定義了上傳數(shù)據的格式,接口地址 http://10.10.10.10:80/restful/personInfo,參數(shù)如下:
msg:{
?? "name" : "fengyuzaitu",
?? "data" : {
????? "id" : "9191"
?? },
?? "sex" : "1",
?? "type" : "worker"
}

代碼
int PostHttpFormDataByLibCurl()
{
?Json::Value root;
?root["type"] = "worker";
?root["sex"] = "1";
?root["name"] = "fengyuzaitu";
?Json::Value data;
?data["id"] = "9191";
?root["data"] = data;
?std::string strPostData = root.toStyledString();
?CURL *pCurlHandle = curl_easy_init();
?std::string strResponseData;
?curl_easy_setopt(pCurlHandle, CURLOPT_CUSTOMREQUEST, "POST");
?curl_easy_setopt(pCurlHandle, CURLOPT_URL, "http://10.10.10.10:80/restful/personInfo");
?curl_easy_setopt(pCurlHandle, CURLOPT_WRITEFUNCTION, WriteResponseBody);//設置回調函數(shù)
?curl_easy_setopt(pCurlHandle, CURLOPT_WRITEDATA, &strResponseData);//設置回調函數(shù)的參數(shù),獲取反饋信息
?struct curl_httppost *pFormPost = 0;
?struct curl_httppost *pLastPtrFormPost = 0;
?curl_formadd(&pFormPost, &pLastPtrFormPost, CURLFORM_COPYNAME, "msg", CURLFORM_COPYCONTENTS, strPostData.c_str(), CURLFORM_END);
?curl_easy_setopt(pCurlHandle, CURLOPT_HTTPPOST, pFormPost);
?CURLcode nRet = curl_easy_perform(pCurlHandle);
?if (0 == nRet)
?{
? std::cout << strResponseData << std::endl;
?}
?curl_formfree(pFormPost);
?curl_easy_cleanup(pCurlHandle);
?return nRet;
}

報文
POST /restful/personInfo HTTP/1.1
Host: 10.10.10.10:80
Accept: */*
Content-Length: 254
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------2630a8c6c773b062
HTTP/1.1 100
--------------------------2630a8c6c773b062
Content-Disposition: form-data; name="msg"
{
?? "name" : "fengyuzaitu",
?? "data" : {
????? "id" : "9191"
?? },
?? "sex" : "1",
?? "type" : "worker"
}
--------------------------2630a8c6c773b062--

備注
??????? 這種表單上傳數(shù)據的方式,也可以通過Content-Type: application/x-www-form-urlencoded的方式進行上傳
代碼
int PostFormDataByUrlEncode()
{
?Json::Value root;
?root["type"] = "worker";
?root["sex"] = "1";
?root["name"] = "fengyuzaitu";
?Json::Value data;
?data["id"] = "9191";
?root["data"] = data;
?std::string strUrl = root.toStyledString();
?CURL *pCurlHandle = curl_easy_init();
?std::string strResponseData;
?curl_easy_setopt(pCurlHandle, CURLOPT_CUSTOMREQUEST, "POST");
?curl_easy_setopt(pCurlHandle, CURLOPT_URL, "http://10.10.10.10:80/restful/personInfo");
?struct curl_slist *pCurlList = NULL;
?//指定文本url編碼
?pCurlList = curl_slist_append(pCurlList, "Content-Type: application/x-www-form-urlencoded");
?curl_easy_setopt(pCurlHandle, CURLOPT_HTTPHEADER, headers);
?curl_easy_setopt(pCurlHandle, CURLOPT_WRITEFUNCTION, WriteResponseBody);//設置回調函數(shù)
?curl_easy_setopt(pCurlHandle, CURLOPT_WRITEDATA, &strResponseData);//設置回調函數(shù)的參數(shù),獲取反饋信息
?char* pszEncodeAuth = curl_easy_escape(pCurlHandle, strUrl.c_str(), strUrl.length());
?std::string strEncodeAuth = pszEncodeAuth;
?//釋放申請的內存
?curl_free(pszEncodeAuth);
?std::string strPostUrlEncodeData = "msg=" + strEncodeAuth;
?curl_easy_setopt(pCurlHandle, CURLOPT_POSTFIELDS, strPostUrlEncodeData.c_str());
?CURLcode nRet = curl_easy_perform(pCurlHandle);
?std::cout << strResponseData << std::endl;
?curl_slist_free_all(pCurlList);
?curl_easy_cleanup(pCurlHandle);
?return nRet;
}

注意:
?std::string strPostUrlEncodeData = "msg=" + strEncodeAuth; 這里的=不能使用:,否則無法解析通過


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI