为什么在尝试 http 发布时会收到 CURLE_URL_MALFORMAT? [英] Why do I get a CURLE_URL_MALFORMAT when trying to http post?
本文介绍了为什么在尝试 http 发布时会收到 CURLE_URL_MALFORMAT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是代码(从现有应用程序中提取):
Here's the code (extracted from an existing application):
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 不兼容吗?
The value of res
is CURLE_URL_MALFORMAT
. Is this URL not compatible with curl?
推荐答案
啊,简单的错误,我需要将 char *
传递给 curl_easy_setopt
而不是 string代码>.为了解决这个问题,我刚刚使用了
.c_str()
像这样:
Ah, simple mistake, I need to pass char *
to curl_easy_setopt
and not string
. To fix this I've just used .c_str()
like so:
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);
这篇关于为什么在尝试 http 发布时会收到 CURLE_URL_MALFORMAT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文