libcurl returns error 3: URL using bad/illegal format or missing URL when using std::string variable [duplicate]

落花浮王杯 提交于 2021-02-07 10:17:29

问题


I'm using libcurl and following the simple https GET tutorial from the libcurl website.

When I hardcode the website URL when setting the CURLOPT_URL option, the request works:

curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com/");
result = curl_easy_perform(curl);
if (CURLE_OK != result)
{ 
    fprintf(stderr, "HTTP REQ failed: %s\n", curl_easy_strerror(result));
}

However, when I put the URL into a std::string and then use the string as the input, it no longer works:

std::string url("https://www.google.com/");
curl_easy_setopt(curl, CURLOPT_URL, url);
result = curl_easy_perform(curl);
if (CURLE_OK != result)
{
    fprintf(stderr, "HTTP REQ failed: %s\n", curl_easy_strerror(result));
}

The request then returns with an error code of 3 (CURLE_URL_MALFORMAT) and the error says:

URL using bad/illegal format or missing URL

What am I missing here for it to work when I directly hardcode the URL but not work when I use a std::string?


回答1:


Curl is a C library. It expects C strings (const char *) not C++ std::string objects.

You want curl_easy_setopt(curl, CURLOPT_URL, url.c_str());



来源:https://stackoverflow.com/questions/51801700/libcurl-returns-error-3-url-using-bad-illegal-format-or-missing-url-when-using

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!