2017-09-10 55 views
0

我在通过C++的curl库上传文件时遇到了崩溃问题。我从这个位置使用确切的演示代码:https://curl.haxx.se/libcurl/c/fileupload.html在CURL的CURL上传文件时curl_easy_perform()发生崩溃

我在代码中改变的唯一一件事是上传位置,上传到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); 

正在导致无效参数的问题。我已经验证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

'/ *设置从哪里读取(在Windows上,您还需要使用READFUNCTION)* /' – tkausl

+0

啊,很容易错过。我添加了行 \t curl_easy_setopt(curl,CURLOPT_READFUNCTION,&fread); 而一切似乎工作 – user2783377

回答

0

感谢Tkausl为察觉行

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

我加入这行我的代码

curl_easy_setopt(curl, CURLOPT_READFUNCTION, &fread); 

而现在一切似乎工作。