Crash on curl_easy_perform() when uploading a file on CURL i

Crash on curl_easy_perform() when uploading a file on CURL in C++

本文关键字:on uploading file CURL when curl easy perform Crash      更新时间:2023-10-16

当通过C 中的curl库上传文件时,我遇到了崩溃的问题。我正在使用此位置的确切演示代码:https://curl.haxx.se/libcurl/c/fileupload.html

我在代码中更改的唯一一件事是上传位置,上传到Windows上的本地WAMP服务器,以及要上传的文件,我已经验证了它的打开确定。

我正在通过Visual Studio 2014跑步,并通过DLL

建立卷曲

该程序的输出为:

*   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);

正在引起无效参数的问题。我已经验证了卷曲变量不是零的,但是我发现很难获得比这更多的调试信息,呼叫堆栈只引用了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: %sn",
                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 secondsn",
                speed_upload, total_time);
        }
        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    fclose(fd);
    return 0;
}

感谢tkausl发现行

/* set where to read from (on Windows you need to use READFUNCTION too) */

我将此行添加到我的代码

curl_easy_setopt(curl, CURLOPT_READFUNCTION, &fread);

现在一切似乎都起作用。