- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以基本上我想做的是使用 libcurl 来获取略有不同的 url,例如:
http://foo.com/foo.asp?name=*NAMEHERE*
我想做的是遍历一个名称 vector 并获取每个名称,例如:
http://foo.com/foo.asp?name=James
然后
http://foo.com/foo.asp?name=Andrew
等等。
但是,当我尝试这样做时:
int foo (){
CURL *curl;
CURLcode success;
char errbuf[CURL_ERROR_SIZE];
int m_timeout = 15;
if ((curl = curl_easy_init()) == NULL) {
perror("curl_easy_init");
return 1;
}
std::vector<std::string> names;
names.push_back("James");
names.push_back("Andrew");
for (std::vector<std::string>::const_iterator i = names.begin(); i != names.end(); ++i){
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, long(m_timeout));
curl_easy_setopt(curl, CURLOPT_URL, "http://foo.com/foo.asp?name=" + *i);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookies.txt");
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1L);
}
if ((success = curl_easy_perform(curl)) != 0) {
fprintf(stderr, "%s: %s\n", "curl_easy_perform", errbuf);
return 1;
}
curl_easy_cleanup(curl);
return 0;
}
它给我一个错误:
Cannot pass object of non-trivial type 'std::__1::basic_string<char>' through variadic function; call will abort at runtime
在这一行:
curl_easy_setopt(curl, CURLOPT_URL, "http://foo.com/foo.asp?name=" + *i);
因为 + *i
。
我想做的事可行吗?有解决办法吗?
编辑:感谢您的回答,但出于某种原因,当我运行它时,它只会获取 vector 中最后一个字符串的网站,而忽略其他字符串。在我的例子中,它跳过了 James
并直接转到了 Andrew
。为什么会这样?
最佳答案
CURLOPT_URL
传递给 curl_easy_setopt
的参数需要是 char *
而不是 std::string
。您可以通过调用 c_str
成员函数从 std::string
获取 const char *
:
std::string url = "http://foo.com/foo.asp?name=" + *i;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
对于将来来到这里的任何人,请查看 curl manual如果你还没有。还有一个 online book .
curl_easy_setopt 的文档表示您需要阅读特定选项以了解要使用的参数类型。
All options are set with an option followed by a parameter. That parameter can be a long, a function pointer, an object pointer or a curl_off_t, depending on what the specific option expects. Read this manual carefully as bad input values may cause libcurl to behave badly!
CURLOPT_URL 的文档准确说明参数类型需要是什么。
Pass in a pointer to the URL to work with. The parameter should be a char * to a zero terminated string which must be URL-encoded in the following format:
scheme://host:port/path
关于c++ - LibCurl CURLOPT_URL 不接受字符串? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23721344/
我是curl 库的新手。谷歌解决了我的大部分问题,例如如何执行 curl 。然而,有一件事仍然让我困惑,那就是当我们设置时 curl_setopt($ch,CURLOPT_URL,'http://ex
所以基本上我想做的是使用 libcurl 来获取略有不同的 url,例如: http://foo.com/foo.asp?name=*NAMEHERE* 我想做的是遍历一个名称 vector 并获取每
我在 CURLOPT_URL 中传递我的变量时遇到问题,我尝试了不同的语法,但到目前为止没有好的结果,所以我希望你能帮助我。 这是我的代码: 'MyCallback' . substr($roomI
如果我有一个如下所示的 URL: $url = 'http://domain.com/?foo=bar'; 然后执行curl如下: $resource = curl_init(); curl_seto
我有这个 php 代码,它使用 cURL 来解析来自 Indeed.com 的 xml 提要。我将 REMOTE_ADDR 和 HTTP_USER_AGENT 等服务器信息传递到 url 的参数中,但
我是一名优秀的程序员,十分优秀!