我在C ++中通过curl库上传文件时出现崩溃问题。我使用了此位置的确切演示代码:https://curl.haxx.se/libcurl/c/fileupload.html
我在代码中唯一更改的内容是上传位置,上传到Windows上的本地wamp服务器以及要上传的文件,我已经确认其开放状态正常。
我正在浏览visual studio 2014,并通过DLL构建CURL
该计划的输出是:
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> PUT /replayupload.php HTTP/1.1
Host: 127.0.0.1
Accept: */*
Content-Length: 43
Expect: 100-continue
< HTTP/1.1 100 Continue
*然后我在程序第66行遇到了崩溃。看来这一行:
res = curl_easy_perform(curl);
导致参数无效的问题。我已经验证了curl变量不是null,但我发现很难获得调试信息,调用堆栈只是引用了DLL中的内存地址。
我能够运行演示只是上传帖子变量并获得一个页面,这样可以正常运行而不会崩溃。仅在上载文件时发生崩溃。
我的确切代码是:
int main(void)
{
CURL *curl;
CURLcode res;
struct stat file_info;
double speed_upload, total_time;
FILE *fd;
fd = fopen("E:\\testfiles\\test.txt", "rb"); /* open file to upload */
if (!fd)
return 1; /* can't continue */
/* to get the file size */
if (fstat(_fileno(fd), &file_info) != 0)
return 1; /* can't continue */
curl = curl_easy_init();
if (curl) {
/* upload to this place */
curl_easy_setopt(curl, CURLOPT_URL,
"http://127.0.0.1/testupload.php");
/* tell it to "upload" to the URL */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* set where to read from (on Windows you need to use READFUNCTION too) */
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
/* and give the size of the upload (optional) */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)file_info.st_size);
/* enable verbose for easier tracing */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
else {
/* now extract transfer info */
curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",
speed_upload, total_time);
}
/* always cleanup */
curl_easy_cleanup(curl);
}
fclose(fd);
return 0;
}
答案 0 :(得分:0)
感谢Tkausl发现了这条线
/* set where to read from (on Windows you need to use READFUNCTION too) */
我将此行添加到我的代码
curl_easy_setopt(curl, CURLOPT_READFUNCTION, &fread);
现在一切似乎都有效。