为什么我在尝试http post时会得到CURLE_URL_MALFORMAT?

时间:2009-12-01 18:00:40

标签: http post curl

这是代码(从现有应用程序中提取):

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不兼容吗?

1 个答案:

答案 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);