#include <string>
#include <iostream>
#include <curl/curl.h>
1.相关代码实现
//使用示例
/*
int main(int argc, char *argv[]) {
CURLcode code;
std::string r1 = get("http://qtdebug.com/html/data.json");
qDebug() << QString::fromUtf8(r1.data());
qDebug() << "------------------------------------------------";
std::string r2 = post("http://eplatform.edu-edu.com.cn/live/api/auth/login", "{ \"username\": \"u1\", \"password\": \"abcd\"}", true);
qDebug() << QString::fromUtf8(r2.data());
qDebug() << "------------------------------------------------";
std::string r3 = get("http://eplatform.edu-edu.com.cn/live/api/channels/mine", &code, {"Authorization: JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjU4NjM1Y2Y4NGY0N2M4MGYyNGI1NDQ5NyIsImlhdCI6MTUwOTA3NjY5MCwiZXhwIjoxNTA5MTYzMDkwfQ.6nLBnhjTYJgwjwFf_Lf0LreKryrQ6ITdT-PcGAPhKB8"});
qDebug() << QString::fromUtf8(r3.data());
return 0;
}
*/
// curl 读取到的数据保存到 std::string
size_t curlSaveResponseToStdString(void *contents, size_t size, size_t nmemb, std::string *s) {
size_t newLength = size * nmemb;
size_t oldLength = s->size();
s->resize(oldLength + newLength);
std::copy((char*)contents, (char*)contents+newLength, s->begin()+oldLength);
return size * nmemb