2014-08-28 67 views
0

我正在使用libcurl並能夠receive json responsesaving it to file如何從服務器接收zip文件:C/C++

下面是代碼

#include <curl/curl.h> 
    #include <curl/types.h> 
    #include <curl/easy.h> 
    #include <string.h> 

    #define URL "http://www.joes-hardware.com/tools.html" 
    size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { 
     size_t written; 
     written = fwrite(ptr, size, nmemb, stream); 
     return written; 
    } 

    int main(void) { 
     CURL *curl; 
     FILE *fp; 
     CURLcode res; 
     //char *url = "http://www.joes-hardware.com/tools.html"; 

     char *url= URL; 
     char outfilename[FILENAME_MAX] = "./json"; 
     curl = curl_easy_init(); 
     if (curl) { 
      fp = fopen(outfilename,"wb"); 
      curl_easy_setopt(curl, CURLOPT_URL, url); 
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); 
      curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); 
      res = curl_easy_perform(curl); 
      curl_easy_cleanup(curl); 
      fclose(fp); 
     } 
     return 0; 
    } 

作爲我開發的應用程序的一部分,現在它必須從服務器獲取一個zip文件。

假設URL如下格式:

#define URL "https://Server/File.zip" 

對於這樣的URL,代碼不能夠保存zip文件。 如何做到這一點?

我正在爲LINUX平臺開發。

+0

你檢查的*您在此列表中每調用API *,看看結果碼他們有失敗?特別是'fopen'和'curl_easy_perform',但是* all *都不是一個壞主意。並且'https' url的設置可能比簡單的拉動複雜得多。 [見此](http://curl.haxx.se/libcurl/c/simplessl.html)。 – WhozCraig 2014-08-28 04:36:39

+0

@WhozCraig對於'http' request say ....'「http://www.joes-hardware.com/tools.html」'',代碼工作絕對正常。「它將json響應保存到相應的文件中。但請求'https:// Server/File.zip'不保存 – 2014-08-28 04:48:06

回答

0

解決了問題。基於下面的鏈接問題是與HTTPS connection 新增certificate

Can't connect to HTTPS site using cURL. Returns 0 length content instead. What can I do?

Getting no content from a HTTPS connection using CURL

#define CURL_STATICLIB 
#include <stdio.h> 
#include <curl/curl.h> 
#include <curl/types.h> 
#include <curl/easy.h> 
#include <string.h> 
#include <stdlib.h> 


#define false 0 

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { 
    size_t written; 
    written = fwrite(ptr, size, nmemb, stream); 
    return written; 
} 


int main(void) { 
    CURL *curl; 
    FILE *fp; 
    CURLcode res; 
    //char *url = "http://www.joes-hardware.com/tools.html"; 

    char *url= "https://HOST/FileName.zip"; 
    //char *url= "https://wordpress.org/"; 
    char outfilename[FILENAME_MAX] = "./json.zip"; 


    curl_version_info_data * vinfo = curl_version_info(CURLVERSION_NOW); 

    if(vinfo->features & CURL_VERSION_SSL){ 
     printf("CURL: SSL enabled\n"); 
    }else{ 
     printf("CURL: SSL not enabled\n"); 
    } 


    curl = curl_easy_init(); 
    if (curl) { 
     fp = fopen(outfilename,"wb"); 
     curl_easy_setopt(curl, CURLOPT_URL, url); 
     /* Setup the https:// verification options - note we do this on all requests as there may 
      be a redirect from http to https and we still want to verify */ 
     //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1); 
     curl_easy_setopt(curl, CURLOPT_CAINFO, "./ca-bundle.crt"); 
     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); 
     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); 

     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); 

     curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); 
     res = curl_easy_perform(curl); 
     curl_easy_cleanup(curl); 


     int i=fclose(fp); 
     if(i==0) 
     system("unzip -j json.zip"); 
    } 
    return 0; 
} 
+0

我不知道爲什麼這不適用於DropBox或谷歌驅動程序公共links.Do你有一個想法? – 2018-02-26 19:51:06

相關問題