我有一个curl脚本:
curl -k -H "Content-Type: application/txt" -X POST --data-binary "@name_file.txt" -g "https://ya.com/file.txt" > .\out.txt
我可以通过控制台ant使用它,效果很好。
我在C ++的Visual Studio中实现了此脚本:
auto curl = curl_easy_init();
if (curl)
{
FILE* fp = fopen(path_to_file.c_str(), "wb");
curl_easy_setopt(curl, CURLOPT_URL, "https://ya.com/file.txt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
struct curl_slist *list = NULL;
list = curl_slist_append(list, "Content-Type: application/txt");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); // - H
std::string data_for_send = data_for_send_arr.c_str();
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data_for_send.size());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data_for_send.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callbackfunction);
res = curl_easy_perform(curl);
if (res != CURLE_OK) AfxMessageBox(_T("error"));
curl_easy_cleanup(curl);
curl_global_cleanup();
fclose(fp);
}
size_t callbackfunction(void *ptr, size_t size, size_t nmemb, void* userdata)
{
FILE* stream = (FILE*)userdata;
if (!stream)
{
printf("!!! No stream\n");
return 0;
}
size_t written = fwrite((FILE*)ptr, size, nmemb, stream);
return written;
}
我还需要告诉我,我使用异步调用: async(func_curl_1,data_1); async(func_curl_2,data_2);
每个函数都会创建curl并同时请求服务器。
,但是此脚本不起作用。它从七个请求开始工作一次 服务器为我返回了空文件或损坏的文件。
我不知道为什么...
答案 0 :(得分:0)
尝试添加您的设置CURLOPT_VERBOSE 即
/* ask libcurl to show us the verbose output */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
并调查输出