• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

libcurl库实现post数据上传

武飞扬头像
欧特克_Glodon
帮助1

具体用例可以从postman进行复制,比较方便,后台接口api写好以后,在postman运行,然后点击右侧编辑按钮,下拉框选择C-libcurl,下面会自动显示出示例代码:
学新通


struct fileInfo  
{  
	fileInfo()
	{
	}

	fileInfo::fileInfo(const fileInfo& other)
	{
		strFilePath = other.strFilePath;
		strFileDir = other.strFileDir;
	}

	fileInfo& operator = (fileInfo& thInfo)
	{
		strFilePath = thInfo.strFilePath;
		strFileDir = thInfo.strFileDir;

		return* this;
	}

	~fileInfo()
	{

	}

	wstring strFilePath;
	wstring strFileDir;
};  


bool CurlPostFile(LPCTSTR sUrl, LPCTSTR sToken,LPCTSTR sProjectId,bool bZip,std::vector<fileInfo> vecFileInfo,int& nStatus)
{
	CURL *curl;
	CURLcode res;
	curl = curl_easy_init();
	if(curl == nullptr)
	{
		return false;
	}

	curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
	curl_easy_setopt(curl, CURLOPT_URL, sUrl);
	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
	curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "http");

	struct curl_slist *headers = NULL;
	headers = curl_slist_append(headers, sToken);
	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
	curl_mime *mime;
	curl_mimepart *part;
	mime = curl_mime_init(curl);

	for (auto iterFile = vecFileInfo.begin(); iterFile != vecFileInfo.end();   iterFile)
	{
		wstring wsAbsFileName = iterFile->strFilePath;
		part = curl_mime_addpart(mime);
		curl_mime_name(part, "files");
		string sFilePath = UnicodeToUtf8(wsAbsFileName);
		curl_mime_filedata(part, sFilePath.c_str());
	}

	string sFilePaths;
	for (int i = 0; i < vecFileInfo.size();   i)
	{
		wstring wsDir = vecFileInfo[i].strFileDir;
		string sDir = UnicodeToASNI(wsDir.c_str());
		sDir = "\""   sDir   "\"";

		if (i != vecFileInfo.size() - 1)
		{
			sFilePaths = sFilePaths   sDir   ",";
		}
		else
		{
			sFilePaths = sFilePaths   sDir;
		}
	}

	sFilePaths = "["   sFilePaths   "]";
	ReplaceString(sFilePaths, "\\", "/");
	wstring wsFilePaths = ANSIToUnicode(sFilePaths);
	string sUTF8Paths = UnicodeToUtf8(wsFilePaths);

	std::wstring wsFilesDir;
	part = curl_mime_addpart(mime);
	curl_mime_name(part, "filePaths");
	curl_mime_data(part, sUTF8Paths.c_str(), CURL_ZERO_TERMINATED);

	part = curl_mime_addpart(mime);
	curl_mime_name(part, "projectId");
	curl_mime_data(part, sProjectId, CURL_ZERO_TERMINATED);

	part = curl_mime_addpart(mime);
	curl_mime_name(part, "needUnZip");
	if (bZip)
	{
		curl_mime_data(part, "true", CURL_ZERO_TERMINATED);
	}
	else
	{
		curl_mime_data(part, "false", CURL_ZERO_TERMINATED);
	}

	curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
	res = curl_easy_perform(curl);
	curl_mime_free(mime);

	curl_easy_cleanup(curl);

	nStatus = (int)res;

	if (res != CURLE_OK)
	{
		return false;
	}

	return true;
}

post数据格式:json,示例如下:

CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
  curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
  curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8082/Reg/Ad");
  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
  struct curl_slist *headers = NULL;
  headers = curl_slist_append(headers, "Content-Type: application/json");
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  const char *data = "{\r\n    \"prCe\":\"ro\",\r\n    \"parentId\":0,\r\n    \"depot\":\"ecp\",\r\n    \"reon\":\r\n    {\r\n        \"name\":\"ds\",\r\n        \"height\":\"2\",\r\n        \"dir\":\"0\",\r\n        \"points\":[5,10,0,11,11,0,6,12,0,10,8,0]\r\n    }\r\n}";
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
  res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfjabg
系列文章
更多 icon
同类精品
更多 icon
继续加载