注册CSDN有十几年了,第一次发文。写博客不求有没有人看,最大的好处是可以记录一些心得。
最近研究CURL,第一步就卡了一天。代码如下:
string operation="";
curl_easy_setopt(curl,CURLOPT_URL, "http://localhost/index2.php?username=superdos&password=123");
curl_easy_setopt(curl,CURLOPT_POST, true);
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,operation.c_str());
curl_easy_setopt
(curl,
CURLOPT_FOLLOWLOCATION
,
1
L);
curl_easy_setopt
(curl,
CURLOPT_WRITEFUNCTION
,&
HelloWorld
::
writeFunction
);
curl_easy_setopt
(curl,
CURLOPT_WRITEDATA
, &buffer);
curl_easy_setopt
(curl,
CURLOPT_TIMEOUT_MS
,
5000
);
ret =
curl_easy_perform
(curl);
if
(ret==
CURLE_OK
)
{
log
(
"ok"
);
}
curl_easy_cleanup
(curl);
其中writeFunction的声明: size_t HelloWorld::writeFunction(void* ptr,size_t size,size_t number,void *stream);
调适过程中发现,虽然curl_easy_perform()返回正常,但是writeFunction函数中,输入参数的内容不正常。ptr,stream输入的不是内存地址,而是返回数据的长度及批次。number反而是一个内存地址。
后来想到在模块间传递类成员函数指针,必须是static的,于是在write Function的声明前加上static后正常。