Libcurl笔记三

时间:2021-09-26 21:09:27

一,post请求和回报处理

//"host/path?extra"
//strHttp=" http://portal.liuhan.com:/web/getConfig?userName=liuhan01 &clientVersion=1.5.0.0";
Curl m_objUrlParser;//ATL的处理类。
m_objUrlParser.CrackUrl(strHttp); // "portal.liuhan.com" + "/web/getConfig"
std::string strUrl = std::string(m_objUrlParser.GetHostName()) + std::string(m_objUrlParser.GetUrlPath());
// "?userName=liuhan01 &clientVersion=1.5.0.0"
std::string strObject = std::string(m_objUrlParser.GetExtraInfo());
if (strObject.at() == '?')
{
strObject.erase(, );
} curl_easy_setopt(m_easyHandle, CURLOPT_URL, strUrl.str());
curl_easy_setopt(m_easyHandle, CURLOPT_POST, 1L);
curl_easy_setopt(m_easyHandle, CURLOPT_VERBOSE, 1L); curl_easy_setopt(m_easyHandle, CURLOPT_POSTFIELDS, strObject.c_str());
curl_easy_setopt(m_easyHandle, CURLOPT_POSTFIELDSIZE, strObject.size()); curl_easy_setopt(m_easyHandle, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(m_easyHandle, CURLOPT_WRITEDATA, pagefile);
    二,upload上传内存中的数据
Libcurl笔记三
网查了好久read_callback全是FILE*的处理,需要研究下。
1:CURLOPT_READFUNCTION设置后read_callback会一直被调用直到read_callback返回值为0为止;
2:第三个参数nitems表示buffer的最大缓冲长度。
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t retcode = fread(ptr, size, nmemb, stream);
return retcode;
}
会一直回调读取文件流stream的内容,直到读到末尾返回读取长度为0时才结束。
typedef struct UploadBuffer
{
char* pBuffs;
unsigned int uOverallLength;
unsigned int uCurrentPos;
} UPLOAD_BUFFER; static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
UPLOAD_BUFFER* pBuffer = (UPLOAD_BUFFER*)stream;
size_t len = pBuffer->uOverallLength- pBuffer->uCurrentPos;
len = len < nmemb ? len : nmemb - ;
memcpy_s(ptr, nmemb, pBuffer->pBuffs+ pBuffer->uCurrentPos, len);
pBuffer->uCurrentPos += len; return len;
} curl_easy_setopt(m_easyHandle, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(m_easyHandle, CURLOPT_READFUNCTION, read_callback);
curl_easy_setopt(m_easyHandle, CURLOPT_URL, UPLOAD_URL);
curl_easy_setopt(m_easyHandle, CURLOPT_READDATA, pUpBuffer);