Libcurl HTTP post发送数据到服务器
libcurl http post send data to server
curl_easy_setopt(curl, CURLOPT_URL, "127.0.0.1:8081/get.php");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,"pulse=70 & temp=35" );
上面的代码运行成功,但是当我传入这个
int pulsedata = 70;
int tempdata = 35;
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "pulse=pulsedata & temp = tempdata");
当我运行上面这行时,它给我错误我怎么能传递这个脉冲数据和tempdata ??
一个可能的C解决方案:
char sendbuffer[100];
snprintf(sendbuffer, sizeof(sendbuffer), "pulse=%d&temp=%d", pulsedate, tempdata);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, sendbuffer);
你不能在这样的字符串中使用变量,你必须格式化字符串。
一个可能的c++解决方案是像这样使用std::ostringstream
:
std::ostringstream os;
os << "pulse=" << pulsedata << "&temp=" << tempdata;
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, os.str().c_sr());
使用这种解决方案,std::ostringstream
对象(在我的示例中是os
)需要在CURL调用全部完成之前保持活动。
还要注意,我构造的查询字符串不包含任何空格。
相关文章:
- 将相机数据从服务器实时流式传输到客户端
- 不将数据 socket.io c++(客户端)发送到 nodejs(服务器)socket.io
- Arduino UNO+SIM800L-向服务器发送数据
- 在 1 个服务器 n 客户端套接字 C++ MFC 应用程序中更新数据的客户端
- TCP 服务器的异步读取使用 boost::asio 打印客户端套接字发送的数据
- Java 客户端C++服务器数据发送/接收问题
- QTcpSocket 在 RemoteHostClosedError 后重新连接到服务器时无法传输数据
- 为 Sql 服务器实现 odbc 包装器.将数据库数据读取为字符或要求驱动程序将数据转换为 C 类型
- 如何使用软化工具包从 OPC UA 服务器异步读取操作回调中的数据值响应中获取 NodeId 详细信息
- 如何在C++中使用带有SFML的http reqest从节点.js服务器获取数据?
- 在 MySQL 中运行 HTTP 服务器以从客户端接收数据
- 使用 Broadcast 发出的从节点服务器发送的数据不能被 C++ 套接字 IO 客户端读取
- 如何使用 asio 本地数据报构建回显服务器和客户端?
- 将数据包从C++服务器发送到NodeJs服务器时出现MessagePack解码错误
- 如何使用gRPC在客户端和服务器之间双向发送和接收流元数据
- 通过TCP将数据从C++客户端发送到JSON服务器
- 如何使客户端在将数据写入 C++ 管道之前检查服务器是否完成了从管道的读取操作
- C++中从客户端到服务器的数据传输问题
- Qt TCP服务器不从客户端读取数据
- 如何使用 C++ 中的 cpprestsdk 发布 json 数据并从本地服务器获取响应