<bdo id='vRRA6'></bdo><ul id='vRRA6'></ul>
    1. <legend id='vRRA6'><style id='vRRA6'><dir id='vRRA6'><q id='vRRA6'></q></dir></style></legend>

        <small id='vRRA6'></small><noframes id='vRRA6'>

        <i id='vRRA6'><tr id='vRRA6'><dt id='vRRA6'><q id='vRRA6'><span id='vRRA6'><b id='vRRA6'><form id='vRRA6'><ins id='vRRA6'></ins><ul id='vRRA6'></ul><sub id='vRRA6'></sub></form><legend id='vRRA6'></legend><bdo id='vRRA6'><pre id='vRRA6'><center id='vRRA6'></center></pre></bdo></b><th id='vRRA6'></th></span></q></dt></tr></i><div id='vRRA6'><tfoot id='vRRA6'></tfoot><dl id='vRRA6'><fieldset id='vRRA6'></fieldset></dl></div>

      1. <tfoot id='vRRA6'></tfoot>

        时间:2019-05-16 标签:c++libcurljsonrest

        时间:2023-08-03
        <i id='V5Qtt'><tr id='V5Qtt'><dt id='V5Qtt'><q id='V5Qtt'><span id='V5Qtt'><b id='V5Qtt'><form id='V5Qtt'><ins id='V5Qtt'></ins><ul id='V5Qtt'></ul><sub id='V5Qtt'></sub></form><legend id='V5Qtt'></legend><bdo id='V5Qtt'><pre id='V5Qtt'><center id='V5Qtt'></center></pre></bdo></b><th id='V5Qtt'></th></span></q></dt></tr></i><div id='V5Qtt'><tfoot id='V5Qtt'></tfoot><dl id='V5Qtt'><fieldset id='V5Qtt'></fieldset></dl></div>

          <small id='V5Qtt'></small><noframes id='V5Qtt'>

              <bdo id='V5Qtt'></bdo><ul id='V5Qtt'></ul>

                • <tfoot id='V5Qtt'></tfoot>

                  <legend id='V5Qtt'><style id='V5Qtt'><dir id='V5Qtt'><q id='V5Qtt'></q></dir></style></legend>
                    <tbody id='V5Qtt'></tbody>
                  本文介绍了时间:2019-05-16 标签:c++libcurljsonrest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试使用 libcurl 从 C++ 中的 REST 网页下载 json 文件.如果我转到网页,以下代码有效,但如果我尝试访问 json 则无法下载....

                  I am trying to download a json file from a REST webpage in C++ with libcurl. The following code works if I go to the webpage but it doesnt download if I try to access the json ....

                  我认为这应该是一个简单的修复,但我找不到任何对此的参考......

                  I think it should be an easy fix but I cant find any reference to this ...

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

                  If I go to webpage it opens the json but this code only returns text/html; charset=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
                  ", ct);
                      }
                  }
                  /* always cleanup */ 
                  curl_easy_cleanup(curl);
                  

                  推荐答案

                  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;
                  
                  }   
                  

                  这篇关于时间:2019-05-16 标签:c++libcurljsonrest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将 cURL 内容结果保存到 C++ 中的字符串中 下一篇:curl WRITEFUNCTION 和类

                  相关文章

                  最新文章

                  <small id='91x2O'></small><noframes id='91x2O'>

                • <legend id='91x2O'><style id='91x2O'><dir id='91x2O'><q id='91x2O'></q></dir></style></legend>

                  <tfoot id='91x2O'></tfoot>
                • <i id='91x2O'><tr id='91x2O'><dt id='91x2O'><q id='91x2O'><span id='91x2O'><b id='91x2O'><form id='91x2O'><ins id='91x2O'></ins><ul id='91x2O'></ul><sub id='91x2O'></sub></form><legend id='91x2O'></legend><bdo id='91x2O'><pre id='91x2O'><center id='91x2O'></center></pre></bdo></b><th id='91x2O'></th></span></q></dt></tr></i><div id='91x2O'><tfoot id='91x2O'></tfoot><dl id='91x2O'><fieldset id='91x2O'></fieldset></dl></div>

                      • <bdo id='91x2O'></bdo><ul id='91x2O'></ul>