码迷,mamicode.com
首页 > Web开发 > 详细

如何用CURL并解释JSON

时间:2014-12-31 00:52:23      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:

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\n", 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;
            }
      }

}
 

如何用CURL并解释JSON

标签:

原文地址:http://www.cnblogs.com/redmondfan/p/4194782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!