使用 C++ 中的预定义变量发出 cURL GET 请求
Making a cURL GET request using pre-defined variables in C++
我目前正在macOS上编写一个C++程序,该程序要求我们从用户那里获取两个变量,即HWID和IP地址,并在这样的get请求中使用它们;
CURL* curl;
string result;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "website.com/c.php?ip=" + ip + "&hwid=" + hwid);
这就是hwid
和ip
的定义;
auto hwid = al.exec("ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/ { print $3; }'");
auto ip = al.exec("dig +short myip.opendns.com @resolver1.opendns.com.");
请记住,al.exec 只是一个执行并返回终端命令输出的函数。
但是,执行所有这些操作的问题在于,我为参数提供了curl_easy_setopt不正确的类型......我在发出 GET 请求时遇到这些错误,如前所述;
Cannot pass object of non-trivial type 'basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >' through variadic function; call will abort at runtime
任何帮助将不胜感激。
cURL 库是一个 C 库,它的所有函数都是 C 函数。因此,他们不能像std::string
那样处理对象。当你这样做"website.com/c.php?ip=" + ip + "&hwid=" + hwid
结果是一个std::string
的对象。
解决该问题的一种方法是将"website.com/c.php?ip=" + ip + "&hwid=" + hwid
的结果保存在变量中,然后将该变量与 c_str
函数一起使用以获取 C 样式字符串:
std::string url = "website.com/c.php?ip=" + ip + "&hwid=" + hwid;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
<</div>
div class="answers"> 你应该准备一个const char*
来调用curl_easy_setopt()
:
std::ostringstream oss;
oss << "website.com/c.php?ip=" << ip << "&hwid=" << hwid;
std::string url = oss.str();
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
相关文章:
- 在C++中使用cURL和多线程
- Cpp-Tuple使用带有变量的get
- 通过 get-Method 访问变量在类外不起作用
- 为什么无论你输入什么,这"while(cin.get(str,3))"只运行一次?
- 如何检查cURL是否成功登录?c ++
- Boost.Asio/OpenSSL HTTPS GET certificate trouble
- C++中是否有任何函数等同于python中字典的get函数?
- 带有进度表的 curl 多接口程序
- 使用istringstearm和get行缓慢读取文件
- 如何将 c++ get 函数代码转换为 opencv 算法中使用的 python
- 使用 CURL 上传构建
- 使用 cin.get() 初始化字符数组
- std::variant<>::get() 不能使用 Apple LLVM 10.0 编译
- 使用 curl_easy_cleanup(curl) 时收到未经处理的 NULL 异常
- 在 C++/C 中使用 CURL 发出带有数据文件的 GET HTTP 请求
- 使用 C++ 中的预定义变量发出 cURL GET 请求
- cURL GET请求函数未返回字符串
- 对GET请求使用CURL
- 使用boost::asio发送http GET请求,类似于cURL
- CURL - 如何从 POST 跟踪 GET 重定向