gpt4 book ai didi

c++ - libcurl 在 ubuntu 虚拟机上完美运行,但无法解析 Windows 上的主机

转载 作者:行者123 更新时间:2023-11-30 17:19:41 25 4
gpt4 key购买 nike

在Windows Visual Studio 2013中,curl_easy_perform返回cannot_resolve主机,但同一台电脑中的相同代码在ubuntu虚拟机中工作正常,这是怎么回事?

CURL *curl;
curl_global_init(CURL_GLOBAL_SSL);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_HEADER, 1);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_URL, "https://myhost.com/api/login/");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
// calculate request header
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"username\":\"name\", \"password\":\"test\"}" );
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); // set callback

curl_easy_perform(curl); // perform request
curl_easy_cleanup(curl); // cleanup request
return 0;

程序的详细输出如下

> * STATE: INIT => CONNECT handle 0x907ef0; line 1034 (connection #-5000)
> * Added connection 0. The cache now contains 1 members
> * STATE: CONNECT => WAITRESOLVE handle 0x907ef0; line 1071 (connection #0)
> * Could not resolve host: myhost.com
> * Closing connection 0
> * The cache now contains 0 members
> * Expire cleared

最佳答案

所以我找到了解决方案。问题出在curl_global_init(CURL_GLOBAL_SSL);函数调用。当我将其更改为curl_global_init(CURL_GLOBAL_ALL);程序按预期开始工作。

关于c++ - libcurl 在 ubuntu 虚拟机上完美运行,但无法解析 Windows 上的主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28818955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com