zoukankan      html  css  js  c++  java
  • 如何用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: %s
    ", 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;
                }
          }

    }
     
  • 相关阅读:
    在CSS中,让页面里的字体变清晰,变细
    前端路由优缺点
    HBuilder和HBuilderX有什么区别?
    HTML5有哪些新特性,移除了那些元素?如何处理HTML5新标签的浏览器兼容问题?如何区分HTML和HTML5?
    js中判断奇数或偶数
    遍历数组的方法
    数组的方法
    免费搜索引擎提交(登录)入口大全
    Vue.js详解
    简述JavaScript模块化编程(二)
  • 原文地址:https://www.cnblogs.com/redmondfan/p/4194782.html
Copyright © 2011-2022 走看看