这是代码(从现有应用程序中提取):
CURL *curl = curl_easy_init();
_ASSERTE(curl);
string url = "http://127.0.0.1:8000/";
char *data = "mode=test";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_URL, url);
CURLcode res = curl_easy_perform(curl);
bool success = (res == CURLE_OK);
curl_easy_cleanup(curl);
res
的值为CURLE_URL_MALFORMAT
。此网址与curl不兼容吗?
答案 0 :(得分:1)
啊,简单的错误,我需要将char *
传递给curl_easy_setopt
而不是string
。为了解决这个问题,我刚刚使用了.c_str()
:
CURL *curl = curl_easy_init();
_ASSERTE(curl);
string url = "http://127.0.0.1:8000/";
char *data = "mode=test";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
CURLcode res = curl_easy_perform(curl);
bool success = (res == CURLE_OK);
curl_easy_cleanup(curl);