2017-09-10 58 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); 

而現在一切似乎工作。