下面是使用libcurl库实现文件上传的一个实例:

void CDataProcess::sendFileToServer(void)
{
string netIp = strNetUrl + "SaveFile"; for (int i = ; i < taskZipVector.size(); ++i)
{
string strFileName = taskZipVector[i];
string strFilePath = strAppPath + "\\Task\\"+ strFileName + ".zip"; CURL *curl = NULL;
string readFile = ""; struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
static const char buf[] = "Expect:"; if(- == _access(strFilePath.c_str(),))
{
return ;
}
curl_global_init(CURL_GLOBAL_ALL);
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "fileContent",
CURLFORM_FILE, strFilePath.c_str(),
CURLFORM_END); curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "filename",
CURLFORM_COPYCONTENTS, strFileName.c_str(),
CURLFORM_END); curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "submit",
CURLFORM_COPYCONTENTS, "send",
CURLFORM_END); curl = curl_easy_init();
headerlist = curl_slist_append(headerlist, buf);
curl_easy_setopt(curl, CURLOPT_URL, netIp.c_str()); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
//CFuncs::WriteLogInfo(SLT_ERROR, "finshUpdate() curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
return ;
} isSendedFlag = ; //设置已传送文件标志
curl_easy_cleanup(curl);
curl_formfree(formpost);
curl_slist_free_all (headerlist);
}
}

struct curl_httppost {
  struct curl_httppost *next; /* next entry in the list */
  char *name; /* pointer to allocated name */
  long namelength; /* length of name length */
  char *contents; /* pointer to allocated data contents */
  long contentslength; /* length of contents field, see also
  CURL_HTTPPOST_LARGE */
  char *buffer; /* pointer to allocated buffer contents */
  long bufferlength; /* length of buffer field */
  char *contenttype; /* Content-Type */
  struct curl_slist *contentheader; /* list of extra headers for this form */
  struct curl_httppost *more; /* if one field name has more than one
  file, this link should link to following
  files */
  long flags; /* as defined below */

  /* specified content is a file name */
  #define CURL_HTTPPOST_FILENAME (1<<0)
  /* specified content is a file name */
  #define CURL_HTTPPOST_READFILE (1<<1)
  /* name is only stored pointer do not free in formfree */
  #define CURL_HTTPPOST_PTRNAME (1<<2)
  /* contents is only stored pointer do not free in formfree */
  #define CURL_HTTPPOST_PTRCONTENTS (1<<3)
  /* upload file from buffer */
  #define CURL_HTTPPOST_BUFFER (1<<4)
  /* upload file from pointer contents */
  #define CURL_HTTPPOST_PTRBUFFER (1<<5)
  /* upload file contents by using the regular read callback to get the data and
  pass the given pointer as custom pointer */
  #define CURL_HTTPPOST_CALLBACK (1<<6)
  /* use size in 'contentlen', added in 7.46.0 */
  #define CURL_HTTPPOST_LARGE (1<<7)

  char *showfilename; /* The file name to show. If not set, the
  actual file name will be used (if this
  is a file part) */
  void *userp; /* custom pointer used for
  HTTPPOST_CALLBACK posts */
  curl_off_t contentlen; /* alternative length of contents
  field. Used if CURL_HTTPPOST_LARGE is
  set. Added in 7.46.0 */
};

struct curl_slist {
  char *data;
  struct curl_slist *next;
};

CURL_EXTERN CURLFORMcode curl_formadd(struct curl_httppost **httppost, struct curl_httppost **last_post,.);

libcurl库的简介(二)的更多相关文章

  1. libcurl库的简介(一)

    一.Libcurl库简介 LibCurl是免费的客户端URL传输库,支持FTP,FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE ,LDAP ...

  2. Python数据分析Numpy库方法简介(二)

    数据分析图片保存:vg 1.保存图片:plt.savefig(path) 2.图片格式:jpg,png,svg(建议使用,不失真) 3.数据存储格式: excle,csv csv介绍 csv就是用逗号 ...

  3. Linux之Libcurl库的介绍与应用20170509

    一.LibCurl简介 LibCurl是免费的客户端URL传输库,支持FTP,FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE ,LDAP等 ...

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

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

  5. C++ 用libcurl库进行http通讯网络编程(转)

    转载:http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三. ...

  6. C++ 用libcurl库进行http通讯网络编程[转]

    http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.cur ...

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

    C/C++ 用libcurl库进行http通讯网络编程 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.curl_easy_setopt函数部分选项介绍 四.curl_easy_p ...

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

      一.LibCurl基本编程框架libcurl是一个跨平台的网络协议库,支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议.libcur ...

  9. Raphael Js矢量库API简介:

    Raphael Js矢量库API简介:Raphael Javascript 是一个 Javascript的矢量库. 2010年6月15日,著名的JavaScript库ExtJS与触摸屏代码库项目jQT ...

随机推荐

  1. .NET知识梳理——4.特性Attribute

    1. 特性 1.1        特性Attribute 特性就是一个类,继承自Attribute抽象类(该类无抽象方法.避免实例化),约定俗成用Attribute类结尾,标记时可省略掉Attribu ...

  2. php 对接国外支付 ipay88支付

    ipay88支付 近期接了一个国外的项目,客户指定要这种支付,就搞搞呗,其实流程和思路都是差不多的,往下看 他的流程其实非常简单  下面的流程仔细看看,看懂了就会了 1 首先我们需要先获取下单所需要的 ...

  3. MYSQL碰到The total number of locks exceeds the lock table size 问题解决记录

    解决记录如下: 在mysql里面进行修改操作时提示:The total number of locks exceeds the lock table size ,通过百度搜到innodb_buffer ...

  4. QT版本

    最近在linux下安装qt:发现主要的问题是qt的版本问题:下面来谈谈各个版本的理解 Qt 的版本是按照不同的图形系统来划分的,目前分为五个版本: Win: 适用于Miccrosoft Windows ...

  5. QQ第三方登录(一)

    要实现QQ第三方登陆 这就需要QQ方面的支持. 首先  我们需要在QQ开发者网站上注册,https://connect.qq.com/index.html 登陆之后点击应用管理,正常情况下我们是未提交 ...

  6. ntpq -p命令详解

    ntpq用来监视ntpd操作,ntpq -p查询网络中的NTP服务器,同时显示客户端和每个服务器的关系 [root@localhost ~]# ntpq -p     remote           ...

  7. 压力测试:响应时间、并发、RPS的关系

    需要对服务器接口做压力测试前,要理解的一些术语含义:响应时间.并发.RPS 并发: 什么叫并发?并发不是我们理解的在loadrunner场景中设置并发数,而是正在系统中执行操作或者在系统的队列中排队的 ...

  8. 【填坑】python3 manage.py migrate:?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'

    问题: WARNINGS:?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'        H ...

  9. codeforces 1269D. Domino for Young (二分图证明/结论题)

    链接:https://codeforces.com/contest/1269/problem/D 题意:给一个不规则的网格,在上面放置多米诺骨牌,多米诺骨牌长度要么是1x2,要么是2x1大小,问最多放 ...

  10. crowdfunding项目02——server无法启动

    错误描述:在maven工程下使用tomcat启动项目时,一直报错,排除代码问题(可以打包成功) 原因:jar包在下载过程中网断或者其他原因发生错误,导致server服务无法启动(简单理解:jar包下载 ...