3

我一直在尝试将 curlpp 中的数据发布到 Web 脚本,但它似乎不起作用。在 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; 

我在这里做任何明显错误的事情吗?

4

2 回答 2

2

对于客户端到服务器的上传,该标准指定内容类型将为application/x-www-form-urlencoded或之一multipart/form-data。因为您的 Web 服务器和/或 PHP 遵循标准,所以您应该将内容类型更改为application/x-www-form-urlencoded.

于 2011-07-26T10:34:58.780 回答
0

content-type错了,用application/x-www-form-urlencoded. 见这里

于 2011-07-26T10:29:36.070 回答