libcurl第七课multipart/formdata表单使用-创新互联
场景
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; 这里的=不能使用:,否则无法解析通过
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享标题:libcurl第七课multipart/formdata表单使用-创新互联
网址分享:http://azwzsj.com/article/dhhppd.html