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的更多相关文章

  1. 格式化Curl返回的Json字符

    格式化Curl返回的Json字符 学习了:https://blog.csdn.net/guijiaoba/article/details/78235523 , 使用python工具 curl xxx ...

  2. Curl之Post Json

    curl Post Json $ curl -i -X POST -H "'Content-type':'application/x-www-form-urlencoded', 'chars ...

  3. curl中通过json格式吧post值返回到java中遇到中文乱码的问题

    首先是: curl中模拟http请求: curl -l 127.0.0.1:8080/spacobj/core/do?acid=100 -H "token:101hh" -H &q ...

  4. 命令行获取zabbix最新的issues - jq 解释json

    0.安装jq wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 chmod +x ./jq c ...

  5. curl模拟post json或post xml文件

    转自: https://www.cnblogs.com/xiaochina/p/9750851.html 问题描述: Linux用命令模拟接口,对接口判断!post文件xml/json 问题解决: c ...

  6. php同curl post 发送json并返回json数据实例

    <?php $arr = array( 'subject'=>'课程', 'loginName'=>'Durriya', 'password'=>'123' ); //json ...

  7. curl 命令返回json结构human readable

    在curl命令后面添加 | python -m json.tool 不想显示curl的统计信息,添加 -s参数 例: curl https://news-at.zhihu.com/api/4/news ...

  8. solr curl索引 CSV/Json/xml文件

    在windows系统中,用curl命令工具索引文件命令: 启动solr 在solr-6.6.0\bin的同级目录下的文件夹ImportData下要索引的文件. 1.索引 json文件 curl &qu ...

  9. curl post 用json方式

    if(!function_exists('tps_curl_post3')){ function tps_curl_post3($url, $postData) { $postData = json_ ...

随机推荐

  1. elfinder-2.x的java servlet后端——elfinder-2.x-servlet

    去年在美期间在外导的项目中,需要用到el-finder的完美界面,但苦于没有java后端,因此做了一个elfinder-2.x-servlet. 托管地址:https://github.com/blu ...

  2. 再探Linux动态链接 -- 关于动态库的基础知识

      在近一段时间里,由于多次参与相关专业软件Linux运行环境建设,深感有必要将这些知识理一理,供往后参考. 编译时和运行时 纵观程序编译整个过程,细分可分为编译(Compiling,指的是语言到平台 ...

  3. ###《Effective STL》--Chapter3

    点击查看Evernote原文. #@author: gr #@date: 2014-09-13 #@email: forgerui@gmail.com Chapter3 关联容器 Topic 22: ...

  4. Json字符与Json对象的相互转换

    Json字符与Json对象的相互转换方式有很多,接下来将为大家一一介绍下,感兴趣的朋友可以参考下哈,希望可以帮助到你 1>jQuery插件支持的转换方式: 复制代码 代码如下: $.parseJ ...

  5. (LightOJ 1149) Factors and Multiples

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1149 Description You will be given two sets o ...

  6. I Take It All Back: Using Windows Installer (MSI) Rollback Actions

    Original Link: http://blogs.flexerasoftware.com/installtalk/2011/10/i-take-it-all-back-using-windows ...

  7. HTML5之Canvas画布

    先上代码: <canvas width="1000" height="800">浏览器不支持HTML5!</canvas> <sc ...

  8. 采用thinkphp框架实现添加管理员功能

    最近由于忙于期中和期末考试没有写新的随笔了,另外内心也在纠结要不要考研,直到昨天终于痛下决心,才突然间觉得豁然开朗. 由于做老师留的课程设计作业采用thinkPHP框架频繁,最近的几篇随笔将都从thi ...

  9. 《APUE》第四章笔记(2)

    下面介绍对stat结构的各个成员的操作函数. 先贴个stat结构的图: access函数: #include <unistd.h> int access(const char *pathn ...

  10. Mac OS X平台上Java环境的配置

    最近换了工作,以前是做c/c++的,但是现在公司的主打产品是使用Java开发,为了以后维护代码,现在开始抽空学习一下Java相关的内容. 在学习之前,首先需要搭建各种平台的开发环境,而我选用的操作系统 ...