libcurl returns 错误 3:URL 使用 bad/illegal 格式或在使用 std::string 变量时缺少 URL
libcurl returns error 3: URL using bad/illegal format or missing URL when using std::string variable
我正在使用 libcurl 并遵循来自 libcurl 网站的简单 https GET tutorial。
当我在设置 CURLOPT_URL 选项时对网站 URL 进行硬编码时,请求有效:
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));
}
但是,当我将 URL 放入 std::string 然后使用字符串作为输入时,它不再有效:
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));
}
然后请求 returns,错误代码为 3 (CURLE_URL_MALFORMAT
),错误为:
URL using bad/illegal format or missing URL
当我直接硬编码 URL 但当我使用 std::string 时它不起作用,我在这里错过了什么?
Curl 是一个 C 库。它需要 C 字符串 (const char *
) 而不是 C++ std::string
对象。
你想要curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
我正在使用 libcurl 并遵循来自 libcurl 网站的简单 https GET tutorial。
当我在设置 CURLOPT_URL 选项时对网站 URL 进行硬编码时,请求有效:
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));
}
但是,当我将 URL 放入 std::string 然后使用字符串作为输入时,它不再有效:
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));
}
然后请求 returns,错误代码为 3 (CURLE_URL_MALFORMAT
),错误为:
URL using bad/illegal format or missing URL
当我直接硬编码 URL 但当我使用 std::string 时它不起作用,我在这里错过了什么?
Curl 是一个 C 库。它需要 C 字符串 (const char *
) 而不是 C++ std::string
对象。
你想要curl_easy_setopt(curl, CURLOPT_URL, url.c_str());