libcurl 在 ubuntu 虚拟机上运行完美,但无法解析 windows 上的主机
libcurl work perfect on ubuntu virtual machine but can not resolve host on windows
怎么可能在 windows visual studio 2013 curl_easy_perform returns cannot_resolve host 但同一台电脑中的相同代码在 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);程序按预期开始工作。
怎么可能在 windows visual studio 2013 curl_easy_perform returns cannot_resolve host 但同一台电脑中的相同代码在 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);程序按预期开始工作。