我一直在尝试将 curlpp 中的数据发布到网络脚本中,但它似乎不起作用。在 PHP 端,我只是运行 var_dump($_POST);打印收到的所有东西。 $_POST 全局在执行时显示为空。
我的C++代码如下:
std::stringstream out;
std::stringstream ss;
ss << "127.0.0.1/online.php";
try
{
curlpp::Cleanup clean;
curlpp::Easy request;
curlpp::options::WriteStream ws(&out);
request.setOpt(ws);
request.setOpt<curlpp::options::Url>(ss.str());
request.setOpt(new curlpp::options::Verbose(true));
std::list<std::string> header;
header.push_back("Content-Type: application/octet-stream");
request.setOpt(new curlpp::options::HttpHeader(header));
std::string test = "abcd=abcd";
request.setOpt(new curlpp::options::PostFields(test));
request.setOpt(new curlpp::options::PostFieldSize(test.length()));
request.perform();
}
catch(curlpp::RuntimeError& e)
{
ss.str("");
ss << "Error making request of type " << op << ": " << e.what();
PrintError(ss.str());
return "";
}
std::cout << out.str() << std::endl;
我在这里做错了什么吗?
最佳答案
对于客户端到服务器的上传,standard指定内容类型将是 application/x-www-form-urlencoded
或 multipart/form-data
之一。因为您的 Web 服务器和/或 PHP 遵循标准,所以您应该将您的内容类型更改为 application/x-www-form-urlencoded
。
关于c++ - 来自 curl++ 的 POST 数据不发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6826717/