所以我有一个类在2017年使用curl进行http调用,通过讨论的vcpkg安装:here,使用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;
}
如果我不包含关闭SSL验证的这一行curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
,我会收到错误:curl_easy_perform() failed: Peer certificate cannot be authenticated with given CA certificates
。我尝试按照以下说明安装证书:link,但是说将下载的证书放在与curl.exe相同的文件夹中。据我所知,vcpkg没有安装curl.exe。我找了.crt,我在vcpkg下有一个找到的证书的图像。我应该在哪里放置.crt文件进行身份验证以与Visual Studio一起使用?