libcurl返回错误3:使用错误/非法格式的URL或使用std :: string变量时缺少URL [英] libcurl returns error 3: URL using bad/illegal format or missing URL when using std::string variable

查看:58
本文介绍了libcurl返回错误3:使用错误/非法格式的URL或使用std :: string变量时缺少URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用libcurl,并遵循简单的 https GET教程libcurl网站.

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

当我在设置CURLOPT_URL选项时对网站URL进行硬编码时,请求有效:

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));
}

但是,当我将URL放入std :: string并将字符串用作输入时,它不再起作用:

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));
}

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

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

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

URL using bad/illegal format or missing URL

当我直接对URL进行硬编码但不能使用std :: string时不能正常工作时,我在这里缺少什么?

推荐答案

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

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

您要 curl_easy_setopt(curl,CURLOPT_URL,url.c_str());

这篇关于libcurl返回错误3:使用错误/非法格式的URL或使用std :: string变量时缺少URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆