1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| #include <QDebug> #include <string> #include <iostream> #include <curl/curl.h>
std::string get(const char* url, CURLcode *code = NULL, std::list<const char *> headers = std::list<const char *>()); std::string post(const char* url, const char* data = NULL, bool jsonBody = false, CURLcode *code = NULL, std::list<const char *> headers = std::list<const char *>());
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; }
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; }
std::string get(const char* url, CURLcode *code, std::list<const char *> headers) { std::string response;
CURL *curl = curl_easy_init();
if (curl) { struct curl_slist *tempHeaders = NULL;
std::list<const char *>::const_iterator iter; for (iter = headers.cbegin(); iter != headers.cend(); ++iter) { tempHeaders = curl_slist_append(tempHeaders, *iter); }
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, tempHeaders); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlSaveResponseToStdString); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
CURLcode temp = curl_easy_perform(curl);
if (code != NULL) { *code = temp; }
curl_slist_free_all(tempHeaders); } else { if (code != NULL) { *code = CURLE_FAILED_INIT; } }
curl_easy_cleanup(curl);
return response; }
std::string post(const char* url, const char* data, bool jsonBody, CURLcode *code, std::list<const char *> headers) { std::string response;
CURL *curl = curl_easy_init();
if (curl) { struct curl_slist *tempHeaders = NULL;
std::list<const char *>::const_iterator iter; for (iter = headers.cbegin(); iter != headers.cend(); ++iter) { tempHeaders = curl_slist_append(tempHeaders, *iter); }
if (jsonBody) { tempHeaders = curl_slist_append(tempHeaders, "Accept: application/json; charset=utf-8"); tempHeaders = curl_slist_append(tempHeaders, "Content-Type: application/json"); }
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, tempHeaders); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlSaveResponseToStdString); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); curl_easy_setopt(curl,CURLOPT_POST, 1); curl_easy_setopt(curl,CURLOPT_POSTFIELDS, data);
CURLcode temp = curl_easy_perform(curl);
if (code != NULL) { *code = temp; }
curl_slist_free_all(tempHeaders); } else { if (code != NULL) { *code = CURLE_FAILED_INIT; } }
curl_easy_cleanup(curl);
return response; }
|