189 8069 5689

Libcurl笔记三-创新互联

一,post请求和回报处理

创新互联公司是一家专业提供城子河企业网站建设,专注与成都网站设计、做网站H5建站、小程序制作等业务。10年已为城子河众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。
//"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(0) == '?')
{
    strObject.erase(0, 1);
}

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);
View Code
    二,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;
}
View Code
会一直回调读取文件流stream的内容,直到读到末尾返回读取长度为0时才结束。

typedef struct UploadBuffer
{
char* pBuffs;
    unsignedint uOverallLength;
    unsignedint 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 - 1;
    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);
View Code

新闻标题:Libcurl笔记三-创新互联
网站网址:http://jkwzsj.com/article/ccgcgs.html

其他资讯