curl easy  的使用步骤

curl_easy_init()

curl_easy_setopt()

curl_easy_perform()

curl_easy_cleanup()

------------------------------

//http 返回数据回调

static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
{
std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid);
if( NULL == str || NULL == buffer )
{
return -;
} char* pData = (char*)buffer;
str->append(pData, size * nmemb);
return nmemb;
}

1 post 指定的参数到对应的php页面

  CURL *curl;
CURLcode res;
  std::string strResponse;
curl = curl_easy_init();
if ( !curl )
{
printf("1\n");
return -;
} curl_easy_setopt( curl , CURLOPT_URL ,"http://xxxy.com/xxx.php" );
curl_easy_setopt( curl , CURLOPT_VERBOSE , ) ;
curl_easy_setopt( curl , CURLOPT_POSTFIELDS , "value1=123&value2=345" ); //php服务器页面可以 echo $_POST["value1"]; 输出对应的值
   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
   curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse); 
    res = curl_easy_perform(curl);
if ( res != CURLE_OK )
{
printf("error\n");
return -;
}
curl_easy_cleanup(curl);

2 以表单方式提交数据,上传文件,

char * desUrl = "127.0.0.1/upload.php";

    const char *filePath = "D://110440.jpg"; //文件全路径

    std::string strResponse;
CURL *curl;
CURLcode res; struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL; curl_global_init(CURL_GLOBAL_ALL);   /*文件上传表单域填写 */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "file", //php 端用 $_FILES["file"]取得文件信息
CURLFORM_FILE, filePath,
CURLFORM_END);   /* 表单域填写 */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "value1", //php端用$_POST["value1"] 取得对应值
CURLFORM_COPYCONTENTS, "",
CURLFORM_END); curl = curl_easy_init(); if(curl) { curl_easy_setopt( curl , CURLOPT_URL ,desUrl );
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
curl_easy_setopt(curl, CURLOPT_POST,);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res)); /* always cleanup */
curl_easy_cleanup(curl);
/* then cleanup the formpost chain */
curl_formfree(formpost);

libCurl 简单使用的更多相关文章

  1. autoconf 添加三方库(libcurl)简单试用

    1. 参考项目 https://github.com/rongfengliang/autoconf-project 2. 项目说明 a. 项目结构 ├── Jenkinsfile # jenkins ...

  2. Libcurl最初的实现tfp上传和下载功能

    研究报告指出的目标是使用libcurl实现ftp文件上传和下载功能 一.Libcurlde简要 Libcurl的而且易于使用的利用url进行文件传输的库. , libcurl当前支持DICT, FIL ...

  3. libcurl使用easy模式阻塞卡死等问题的完美解决

    引言: 由于要在android手机测进行DM开发, 其中最重要的就是FUMO和SCOMO下载, 下载使用的是linux开源库libcurl. 于是就把libcurl的使用研究了一遍, 有些心得, 并解 ...

  4. libcurl安装使用方法-简单实用(摘录)

    http://curl.haxx.se/libcurl/c/example.html 官网c例子http://curl.haxx.se/download/curl-7.21.3.tar.gz 下载地址 ...

  5. linux c libcurl的简单使用(转)

    curl是Linux下一个非常著名的下载库,通过这个库,可以很简单的实现文件的下载等操作.看一个简单的例子: #include <curl/curl.h> #include <std ...

  6. libcurl库的简单使用

    #include <stdio.h> #include <tchar.h> #include <windows.h> #include <process.h& ...

  7. libcurl教程

    名称 libcurl 的编程教程 目标 本文档介绍使用libcurl编程的一般原则和一些基本方法.本文主要是介绍 c 语言的调用接口,同时也可能很好的适用于其他类 c 语言的接口. 跨平台的可移植代码 ...

  8. 【转】如何在Windows+VS2005使用最新静态libcurl 7.35.0获取网页数据,支持HTTPS

    地址: http://blog.csdn.net/hujkay作者:Jekkay Hu(34538980@qq.com)关键词:Windows,curl,ssl,  visual c++ 2005, ...

  9. C++ 用libcurl库进行http通讯网络编程

    使用libcurl完成http通讯,很方便而且是线程安全,转载一篇比较好的入门文章 转载自 http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724 ...

随机推荐

  1. #error#storyboard#xib#解决方案

      https://www.evernote.com/shard/s227/sh/cad7d5f5-8e81-4b3b-908f-5d8eee7d11e2/928786149cf9a103a74626 ...

  2. Ubuntu 15.10 安装比特币客户端

    下载 git clone https://github.com/bitcoin/bitcoin.git cd bitcoin ./autogen.sh 安装依赖包: ++-dev sudo apt-g ...

  3. shell命令行混合进制计算器smartbc

    需要简单的计算的时候,不想用GUI的计算器,能在shell下直接计算就最好了 查了下,有个东西叫 bc,  具体的使用就不赘述了,可以运行bc,然后进去计算,也可以echo传递过去,大概是像这样 ec ...

  4. VS mfc MessageBox() 使用英文显示

    转载:http://blog.csdn.net/guoyk1990/article/details/44337249 由于特殊原因我们需要将 MessageBox 或 Dialog 的按钮“确定”.“ ...

  5. React 踩坑记录

    1.React-router error: super expression must either be null or a function 原因:引入babel后写ES6风格的代码: class ...

  6. 搭建Spring框架,实现添加数据到数据库

    原创链接:http://www.cnblogs.com/yanqin/p/5284400.html (允许转载,但请注明原创链接) 1.在myeclipse中建立一个web项目 项目名 :spring ...

  7. 【python】python2.x 与 python3.x区别对照+缩进错误解决方法

    仅仅列出我用到的,不全. 划重点: 1. urllib2 用 urllib.request 代替 2. urllib.urlencode 用 urllib.parse.urlencode 代替 3. ...

  8. python模拟鼠标和键盘操作

    import win32api import win32con import win32gui from ctypes import * import time VK_CODE = { 'backsp ...

  9. hdu 5194(DFS)

    DZY Loves Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  10. 实战WCF中net.tcp和net.msmq绑定协议

    平时很少写博文的,以前都是转载其他园友的文章,这几天有时间就自己尝试写一些wcf相关的文章,希望能给有需要的人带来一点帮助吧,水平有限再加上初次动手,写得不好还请多多包含!废话不多说了直接进入正题. ...