一,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());
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);
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
char* p = (char*)ptr;
size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
return written;
}
?
二,upload上传内存中的数据网查了好久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 uiOverallLength;
????unsigned int uiCurrentPos;
} 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->uiOverallLength - pBuffer->uiCurrentPos;
????len = len < nmemb ? len : nmemb - 1;
????memcpy_s(ptr, nmemb, pBuffer->pBuffs + uiCurrentPos , len);
pBuffer->uiCurrentPos += 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);