libcurl库的使用

Libcurl为一个免费开源的,客户端url传输库,支持FTP,FTPS,TFTP,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE和LDAP,跨平台,支持Windows,Unix,Linux等,线程安全,支持Ipv6。并且易于使用。

开发工具:vs2010

环境:控制台程序

操作系统:win7操作系统

 

 配置如下:

 1、库头文件的引入

2、lib库的引入

 

 

 

 3、依赖项的引入

 4、预处理的定义

 

4

 

 

 

 

 

 库的使用:

在基于LibCurl的程序里,主要采用callback function (回调函数)的形式完成传输任务,用户在启动传输前设置好各类参数和回调函数,当满足条件时libcurl将调用用户的回调函数实现特定功能。下面是利用libcurl完成传输任务的流程:
1.       调用curl_global_init()初始化libcurl
2.       调用 curl_easy_init()函数得到 easy interface型指针
3.       调用curl_easy_setopt设置传输选项
4.       根据curl_easy_setopt设置的传输选项,实现回调函数以完成用户特定任务
5.       调用curl_easy_perform()函数完成传输任务
6.       调用curl_easy_cleanup()释放内存

 

 

代码如下:

 

// NMakeDemo.cpp : 定义控制台应用程序的入口点。
 ***************************************************************************/

#include <stdio.h>
#include <iostream>
#include <curl/curl.h>
using namespace std;
int main(void)
{
  CURL *curl;
  CURLcode res;

  // 初始化libcurl
  res = curl_global_init(CURL_GLOBAL_WIN32);
  if (CURLE_OK != res)
  {
   return -1;
  }
  cout<<curl_version()<<endl;
  curl = curl_easy_init();
  if(curl) {
   // curl_easy_setopt(curl, CURLOPT_URL, "
http://example.com");
   curl_easy_setopt(curl, CURLOPT_URL, "
file:///C:/Users/zfs/Desktop/WebAudio/WebAudio/readme.txt");
 
    /* example.com is redirected, so we tell libcurl to follow redirection */
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}
/***************************************************************************/

运行如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值