【问题标题】:c++ libcurl json restc++ libcurl json rest
【发布时间】:2011-08-08 03:43:09
【问题描述】:

我正在尝试使用 libcurl 从 C++ 中的 REST 网页下载 json 文件。 如果我访问网页,以下代码可以工作,但如果我尝试访问 json,它不会下载 ....

我认为这应该是一个简单的解决方法,但我找不到任何参考...

如果我访问网页,它会打开 json,但此代码仅返回 text/html;字符集=utf-8

????????????

CURL *curl;
CURLcode res;
    struct curl_slist *headers=NULL; // init to NULL is important 
    headers = curl_slist_append(headers, "Accept: application/json");   

curl = curl_easy_init();
if(curl) {

    curl_easy_setopt(curl, CURLOPT_URL, "http://web.com/api/json/123");
            curl_easy_setopt(curl, CURLOPT_HTTPGET,1);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    //curl_easy_setopt(curl, CURLOPT_URL, "http://web.com/123.html");//this works!!!
    res = curl_easy_perform(curl);

    if(CURLE_OK == res) {
        char *ct;
        /* ask for the content-type */
        res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
        if((CURLE_OK == res) && ct)
            printf("We received Content-Type: %s\n", ct);
    }
}
/* always cleanup */ 
curl_easy_cleanup(curl);

【问题讨论】:

    标签: c++ json curl libcurl


    【解决方案1】:
    std::string ServerContent::DownloadJSON(std::string URL)
    {   
        CURL *curl;
        CURLcode res;
        struct curl_slist *headers=NULL; // init to NULL is important 
        std::ostringstream oss;
        headers = curl_slist_append(headers, "Accept: application/json");  
        headers = curl_slist_append(headers, "Content-Type: application/json");
        headers = curl_slist_append(headers, "charset: utf-8"); 
        curl = curl_easy_init();
    
        if (curl) 
        {
            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
            curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
            curl_easy_setopt(curl, CURLOPT_HTTPGET,1); 
            curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writer);
            res = curl_easy_perform(curl);
    
            if (CURLE_OK == res) 
            { 
                char *ct;         
                res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
                if((CURLE_OK == res) && ct)
                    return *DownloadedResponse;
            }
        }
    
        curl_slist_free_all(headers);
    }
    
    
    static std::string *DownloadedResponse;
    
    static int writer(char *data, size_t size, size_t nmemb, std::string *buffer_in)
    {
    
        // Is there anything in the buffer?  
        if (buffer_in != NULL)  
        {
            // Append the data to the buffer    
            buffer_in->append(data, size * nmemb);
    
            // How much did we write?   
            DownloadedResponse = buffer_in;
    
            return size * nmemb;  
        }
    
        return 0;
    
    }   
    

    【讨论】:

    • 你有两个电话给curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); 我相信不需要第二个。
    • 另外,根据我的经验,您似乎应该将 curl_slist_append 的返回值分配回 headers 以修改列表。
    • 您错过了对curl_slist_free(headers) 的调用,因此此代码泄露了标头列表
    • @PeteKirkham 你错过了_all。 curl_slist_free_all(headers);
    • 您的 curl_slist_append 调用没有附加任何内容:您忽略了返回值。
    【解决方案2】:

    我认为这与 HTTP 服务器上的配置有关。首先,您应该发送某种类型的指示,说明您期望什么类型,例如,添加一个 Accept 标头,如下所示:

    Accept: application/json
    

    如果您没有指定预期的内容,服务器可能会在响应标头 Content-Type 中返回默认 HTML,这就是 curl 对您所说的内容。

    【讨论】:

    • 我尝试添加以下内容,但无法从服务器响应中获取实际数据.... struct curl_slist *headers=NULL; // 初始化为 NULL 很重要 headers = curl_slist_append(headers, "Accept: application/json"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    【解决方案3】:

    您是否从 curl 站点的教程代码中尝试过这种方法? http://curl.haxx.se/libcurl/c/sepheaders.html

    【讨论】:

    • 我想读取json而不是放它,是否有CURLOPT_READHEADER或其他东西来读取从服务器返回的数据流?顺便说一句,这对我来说在命令行 curl -i -H "Accept: application/json" -X GET web.com/api/json/123
    猜你喜欢
    • 2012-08-12
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 2012-02-11
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    相关资源
    最近更新 更多