您好,登錄后才能下訂單哦!
場景
???????? multipart/form-data是瀏覽器用表單上傳文件的方式。最常見的情境是:在寫郵件時,向郵件后添加附件,附件通常使用表單添加,也就是用multipart/form-data格式上傳到服務器。Http服務器定義了上傳數據的格式,接口地址 http://10.10.10.10:80/restful/personInfo,參數如下:
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);//設置回調函數
?curl_easy_setopt(pCurlHandle, CURLOPT_WRITEDATA, &strResponseData);//設置回調函數的參數,獲取反饋信息
?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--
備注
??????? 這種表單上傳數據的方式,也可以通過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);//設置回調函數
?curl_easy_setopt(pCurlHandle, CURLOPT_WRITEDATA, &strResponseData);//設置回調函數的參數,獲取反饋信息
?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; 這里的=不能使用:,否則無法解析通過
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。