粗看libcurl

一直想学习下libcurl库,昨天代码写了不少,今天有点空闲,粗看了下libcurl中带的指南,深深感到这套库的灵活和强大。
下面贴点笔记:

libcurl使用:
1、初始化与释放:
curl_global_init()有一个参数,取值为以下的:
CURL_GLOBAL_ALL 这个宏,标识初始化所有对象
CURL_GLOBAL_WIN32 :
WIN32 平台下使用

 

CURL_GLOBAL_SSL :
控制SSL编译你开关。需要SSL时使用

libcurl使用完毕后,需要执行清理工作:
curl_global_init
curl_global_cleanup

2、简单使用
libcurl提供一套curl_easy开头的函数,提供了简单的接口供用户使用
使用前需要创建一个easy_handle作为操作句柄,单个句柄只支持单线程,不要将它放在多线程中使用。

使用curl_easy接口的步骤:
1、初始化返回操作句柄
2、对操作句柄进行设置
3、提交
4、释放操作句柄

初始化curl_easy接口:
 easyhandle = curl_easy_init();
 
设置curl_easy接口,使用的是类似setsockopt的接口:
curl_easy_setopt(handle, CURLOPT_URL, "http://domain.com/");
CURLOPT_URL这是个宏,用来表示设置的是什么属性(URL,提交类型等)

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

其核心就是curl_easy_setopt的第二个参数,其取值决定了第三个值。
简直是无所不能。贴个简单的post代码,来自libcurl官方,稍加修改就可以做成一个刷留言板的回帖机:

 #include <stdio.h>

#include "curl/curl.h"

 

int main(void)

{

  CURL *curl;

  int i = 0;

  CURLcode res;

 

  curl = curl_easy_init();

  if(curl) {

    /* First set the URL that is about to receive our POST. This URL can

       just as well be a https:// URL if that is what should receive the

       data. */ 

    curl_easy_setopt(curl, CURLOPT_URL, "这里得到的是抓包得到的网址");

    /* Now specify the POST data */ 

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "这里填写抓包得到的提交字符串");

 

    /* Perform the request, res will get the return code */ 

	for (i = 0; i < 500; i++)

	{

		res = curl_easy_perform(curl);

		Sleep(5000);

	}





 

    /* always cleanup */ 

    curl_easy_cleanup(curl);

  }

  return 0;

}

你可能感兴趣的:(curl)