当尝试进行HTTP POST时,为什么会出现CURLE_URL_MALFORMAT错误?

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

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

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);
< p > < code > res 的值是 < code > CURLE_URL_MALFORMAT 。这个URL是否不兼容curl?< /p >

m_checkUrl的类型是什么?您是否用char*测试过?例如,在curl_easy_setopt的第三个参数中放置“http://127.0.0.1:8000/”? - John Weldon
该 URL 应该有一个 http: 前缀。 - John Weldon
我已经更新了我的代码示例 - 我使用了字符串而不是char * - 哎呀! - Nick Bolton
1个回答

10

啊,简单的错误,我需要传递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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接