我是靠谱客的博主 认真导师,最近开发中收集的这篇文章主要介绍如何用CURL并解释JSON,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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");//cant get json file
curl_easy_setopt(curl, CURLOPT_URL, "http://web.com/pages/123.html");//this returns entire webpage
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_RETURNTRANSFER, true);
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: %sn", ct);
}
}
// always cleanup
curl_easy_cleanup(curl);



//参考答案1

std::string ServerContent::DownloadJSO
N(std::string URL)
{
      
      CURL *curl;
      CURLcode res;
      struct curl_slist *headers=NULL; // init to NULL is important
      std::ostringstream oss;
       curl_slist_append(headers, "Accept: application/json");  
      curl_slist_append( headers, "Content-Type: application/json");
      curl_slist_append( headers, "charsets: 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_HTTPHEADER, headers);
            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;
            }
      }

}
 

转载于:https://www.cnblogs.com/redmondfan/p/4194782.html

最后

以上就是认真导师为你收集整理的如何用CURL并解释JSON的全部内容,希望文章能够帮你解决如何用CURL并解释JSON所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部