gpt4 book ai didi

c++ - "failed writing body & data"libcurl C++

转载 作者:行者123 更新时间:2023-11-28 03:00:17 24 4
gpt4 key购买 nike

您好,我正在尝试保存 CURL 服务器响应并收到以下错误:我读到这是由于读取和写入文件的数据大小不同,所以 curl 很糟糕。文件已创建并包含需要的 XML 数据,但仍显示错误。感谢您的帮助!!

  • 写入正文失败(0 != 96)

  • 写入数据失败

  • 关闭连接 #0

这是我的 cb 函数:

static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{ size_t written = fwrite(ptr, size, nmemb, stream); return written; }

和方法:

createJob(std::string s, std::string 目录) {

    static const char *fp = "resultcurl.xml";
std::stringstream filepath;
filepath<< directory;
filepath<< fp;

std::string pagefilename = filepath.str();

FILE *pagefile;
std::string job_id;

struct curl_slist *headers = NULL;

std::string jobXML = s;
curl = curl_easy_init();

if (curl) {

headers = curl_slist_append(headers, "Accept: */*");
headers = curl_slist_append(headers, "Content-Type: application/xml");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_VERBOSE, true);

curl_easy_setopt(curl, CURLOPT_URL,
".../jobs.xml");

curl_easy_setopt(curl, CURLOPT_USERPWD, "...");

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jobXML.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(jobXML.c_str()));

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,write_data);

pagefile = fopen(pagefilename.c_str(), "wb");

if (pagefile) {


curl_easy_setopt(curl, CURLOPT_FILE, pagefile);

//curl_easy_perform(curl);


}

res = curl_easy_perform(curl);

/* cleanup curl stuff */
curl_easy_cleanup(curl);

//job_id =readXMLvalue(pagefilename);
fclose(pagefile);
return 0;

}

最佳答案

交换设置的顺序,

   pagefile = fopen(pagefilename.c_str(), "wb");
if (pagefile) {
curl_easy_setopt(curl, CURLOPT_FILE, pagefile);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,write_data);
...
}
...

关于c++ - "failed writing body & data"libcurl C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21042657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com