我們需要從CPP中的curl接收響應字符串,我嘗試了以下選項,但沒有任何工作.js使用xhr.responseText來做這個。我需要在cpp中做。在cpp中檢索curl中的響應文本
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
const char* sp = static_cast<const char*>(contents);
readBuffer.append(sp, realsize);
return realsize;
}
CURLcode res;
char* http_error= new char[100];
readBuffer.clear();
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
CURLcode code(CURLE_FAILED_INIT);
code = curl_easy_perform(curl);
cout << "Curl response msg CURLOPT_WRITEDATA: "<<curl_easy_strerror(code)<< " respose :"<<readBuffer;
res=curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code) ;
cout << "Curl response msg: "<< curl_easy_strerror(res);
也許你應該使用高級別庫https://github.com/whoshuu/cpr –
沒有直接的方法爲此像檢索http狀態碼? – user2769274