【发布时间】:2021-05-25 00:44:21
【问题描述】:
我对 curl 或 HTTP 请求不是很熟悉,但正在努力学习。
就我而言,我正在尝试在 C++ 中使用 libcurl(在 Windows 10 上使用 Visual Studio 2019)来执行 GET 请求。我尝试了Curl in C++ - Can't get data from HTTPS 的解决方案,但唯一对我有用的是使用以下方法禁用 SSL 对等验证:
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
这是我的代码:
void getPairPrice(string & pair) {
string url(BINANCE_HOST);
url += "/api/v3/ticker/price?symbol=";
url += pair;
CURL* curl;
CURLcode res;
std::string readBuffer;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip");
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, PUBLIC_KEY_HEADER);
headers = curl_slist_append(headers, CONTENT_TYPE_HEADER);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
//cout << "res : " << res << endl;
std::cout << "GET symbol " << pair << " price : " << readBuffer << std::endl;
const string jsonKey = "price";
cout << "Price : " << extractJsonValue(readBuffer, jsonKey) << endl;
}
curl_easy_cleanup(curl);
curl_global_cleanup();
}
在不禁用SSL_VERIFYPEER 选项的情况下,响应始终为 77。这对于测试来说很好,但我想知道在发布我的软件时如何解决这个问题。看来我应该以某种方式下载 PEM 格式的主机 SSL 证书并将 libcurl 指向它。
谁能帮忙?
【问题讨论】:
标签: c++ openssl libcurl binance