LibCurl CURLOPT_URL不接受字符串?C++

 手机用户2702932821 发布于 2023-01-14 15:34
  • php
  • 基本上我想要做的是使用libcurl来获取稍微不同的URL,例如:

    http://foo.com/foo.asp?name=*NAMEHERE*
    

    我想做的是遍历名称向量并获取每个名称,例如:

    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 names;
    
        names.push_back("James");
        names.push_back("Andrew");
    
        for (std::vector::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;
    }
    

    它给了我一个错误:

    无法通过可变参数函数传递非平凡类型'std :: __ 1 :: basic_string '的对象; call将在运行时中止

    在这一行:

    curl_easy_setopt(curl, CURLOPT_URL, "http://foo.com/foo.asp?name=" + *i);
    

    因为+ *i.

    我想做什么?有解决方案吗?

    编辑:谢谢你的答案,但由于某种原因,当我运行它时,它只获得带有向量中最后一个字符串的网站,而忽略了其他的.就我而言,它会跳过James并直接进入Andrew.为什么会这样?

    撰写答案
    今天,你开发时遇到什么问题呢?
    立即提问
    热门标签
    PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有