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. bzoj 2039 最小割模型

    比较明显的网络流最小割模型,对于这种模型我们需要先求获利的和,然后减去代价即可. 我们对于第i个人来说, 如果选他,会耗费A[I]的代价,那么(source,i,a[i])代表选他之后的代价,如果不选 ...

  2. bzoj 1191 匈牙利算法

    只需要做一遍匈牙利,只要有一个没法匹配上就break就行了 /************************************************************** Proble ...

  3. Python学习笔记 - day4 - 流程控制

    Python流程控制 Python中的流程控制主要包含两部分:条件判断和循环. Python的缩进和语法 为什么要在这里说缩进和语法,是因为将要学习的条件判断和分支将会涉及到多行代码,在java.c等 ...

  4. Swift : missing argument label 'xxx' in call

    http://stackoverflow.com/questions/24050844/swift-missing-argument-label-xxx-in-call up vote37down v ...

  5. Linux内核设计与实现读书笔记(8)-内核同步方法【转】

    转自:http://blog.chinaunix.net/uid-10469829-id-2953001.html 1.原子操作可以保证指令以原子的方式执行——执行过程不被打断.内核提供了两组原子操作 ...

  6. 浅谈redux 中间件的原理

    在使用redux管理异步数据流的时候,我们会使用中间件,以redux-thunk中间件为例,我们做一下分析: 首先是构建store,我们需要以下代码进行揉入中间件的类似creatStore函数的构造: ...

  7. python--celery

    有些时候我们的一些任务比较耗时,比如我们写了一个网站,用户注册的时候需要发送邮件.但是发送邮件的过程比较耗时,用户必须要等到我们将邮件发送成功之后才会得到响应.那么有没有一种办法,当用户点击发送邮件的 ...

  8. 【C++】this指针

    来自:黄邦勇帅 this 指针是所有成员函数的隐含指针,每次调用成员函数时,this 指针就指向调用此函数的对象.可以在成员函数类 部使用显使用this 指针. 友元函数不是类的成员函数,所以友元函数 ...

  9. Eclipse,myeclipse安装 配置Maven

    本文转自http://www.cnblogs.com/timeng/archive/2013/05/07/maven_install.html myeclipse自带了maven插件,但是和原生插件还 ...

  10. 使用vscode开发调试.net core应用程序并部署到Linux跨平台

    使用VS Code开发 调试.NET Core RC2应用程序,由于.NET Core 目前还处于预览版. 本文使用微软提供的示例进行开发及调试. https://github.com/aspnet/ ...