【问题标题】:libcurl returns error 3: URL using bad/illegal format or missing URL when using std::string variable [duplicate]libcurl 返回错误 3:使用错误/非法格式的 URL 或使用 std::string 变量时缺少 URL [重复]
【发布时间】:2018-08-11 16:33:54
【问题描述】:

我正在使用 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));
}

然后请求返回错误代码 3 (CURLE_URL_MALFORMAT),错误提示:

网址使用错误/非法格式或缺少网址

当我直接硬编码 URL 时我在这里缺少什么,但当我使用 std::string 时它不起作用?

【问题讨论】:

标签: c++ get libcurl


【解决方案1】:

Curl 是一个 C 库。它需要 C 字符串 (const char *) not C++ std::string 对象。

你想要curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

【讨论】:

    猜你喜欢
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多