所以我有一个类在 Visual Studios 2017 中使用 curl 进行 http 调用,该类是通过讨论的 vcpkg 安装的:这里,使用 curl_easy 函数调用:
string returnResponseAsString(string requestURL) {
CURL *curl_handle;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = (char *)malloc(1);
chunk.size = 0;
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/*Turn off SSL Verifcation*/
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, requestURL.c_str());
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
/* some servers don't like requests that are made without a user-agent*/
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
/* get it! */
res = curl_easy_perform(curl_handle);
/* check for errors */
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
else {
printf("%lu bytes retrieved\n", (long)chunk.size);
}
string response = chunk.memory;
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
free(chunk.memory);
/* we're done with libcurl, so clean it up */
curl_global_cleanup();
return response;
}
如果我不包括curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
关闭 SSL 验证的这一行,我会收到错误:curl_easy_perform() failed: Peer certificate cannot be authenticated with given CA certificates
. 我尝试按照以下说明安装证书:链接,但据说将下载的证书放在与 curl.exe 相同的文件夹中。据我所知,vcpkg 没有安装 curl.exe。我查找了 .crt,并在 vcpkg 下找到了找到的证书的图像。我应该在哪里放置 .crt 文件以进行身份验证以与 Visual Studio 一起使用?