我想将curl请求的输出重定向到文件,但是我做不到。有人可以帮助我了解我在做什么错吗?
我一直在寻找可以为我的问题提供答案的每个主题,但是不幸的是,我尝试的所有方法都无法正常工作。
我使用了官方文档中的示例,以及用户提供的其他代码,但对我没有用:输出文件为空。
这是我尝试的最后一个代码:
std::string filename = "answer.out";
curlpp::Cleanup cleaner;
curlpp::Easy request;
std::list<std::string> header;
header.push_back("Connection: keep-alive");
header.push_back("Upgrade-Insecure-Requests: 1");
header.push_back("User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36 OPR/60.0.3255.83");
header.push_back("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
header.push_back("Accept-Encoding: gzip, deflate");
header.push_back("Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7");
header.push_back("Cookie: __test=4558ed530ffeb24f11c7d1812f85b68d; PHPSESSID=efb4f11bd458ed43cefb51be871dbb73");
request.setOpt(new curlpp::options::HttpHeader(header));
curlpp::options::WriteFunctionCurlFunction
myFunction(WriteCallback);
FILE *file = stdout;
if(filename != "")
{
file = fopen(filename.c_str(), "wb");
if(file == NULL)
{
fprintf(stderr, "%s/n", strerror(errno));
}
}
curlpp::OptionTrait<void *, CURLOPT_WRITEDATA>
myData(file);
request.setOpt(myFunction);
request.setOpt(myData);
curlpp::Forms formParts;
formParts.push_back(new curlpp::FormParts::Content("pseudo_connect", "myusername"));
formParts.push_back(new curlpp::FormParts::Content("pass_connect", "mypassword"));
request.setOpt(new curlpp::options::HttpPost(formParts));
request.setOpt(new curlpp::options::Url("http://fpvp.fiddlecomputers.fr?action=connect"));
request.setOpt(new curlpp::options::Verbose(false));
request.setOpt(new curlpp::options::Encoding(""));
request.perform();
WriteCallBack函数:
size_t WriteCallback(char* ptr, size_t size, size_t nmemb, void *f)
{
FILE *file = (FILE *)f;
return fwrite(ptr, size, nmemb, file);
};
我没有收到任何错误,但是输出文件(即answer.out)仍然为空。 预先感谢您的帮助。