天天看點

curl 庫的使用

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

struct MemoryStruct {
  char *memory;
  size_t size;
};

static void *myrealloc(void *ptr, size_t size);

static void *myrealloc(void *ptr, size_t size)
{
  /* There might be a realloc() out there that doesn't like reallocing
     NULL pointers, so we take care of it here */
  if(ptr)
    return realloc(ptr, size);
  else
    return malloc(size);
}

static size_t
WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
  size_t realsize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)data;

  mem->memory = myrealloc(mem->memory, mem->size + realsize + 1);
  if (mem->memory) {
    memcpy(&(mem->memory[mem->size]), ptr, realsize);
    mem->size += realsize;
    mem->memory[mem->size] = 0;
  }
  return realsize;
}

int main(int argc, char **argv)
{
  CURL *curl_handle;

  struct MemoryStruct chunk;

  chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
  chunk.size = 0;    /* no data at this point */

  curl_global_init(CURL_GLOBAL_ALL);

  /* init the curl session */
  curl_handle = curl_easy_init();

  /* specify URL to get */
  curl_easy_setopt(curl_handle, CURLOPT_URL, "http://cool.haxx.se/");

  /* send all data to this function  */
  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

  /* we pass our 'chunk' struct to the callback function */
  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);

  /* some servers don't like requests that are made without a user-agent
     field, so we provide one */
  curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");

  /* get it! */
  curl_easy_perform(curl_handle);

  /* cleanup curl stuff */
  curl_easy_cleanup(curl_handle);

  /*
   * Now, our chunk.memory points to a memory block that is chunk.size
   * bytes big and contains the remote file.
   *
   * Do something nice with it!
   *
   * You should be aware of the fact that at this point we might have an
   * allocated data block, and nothing has yet deallocated that data. So when
   * you're done with it, you should free() it as a nice application.
   */

  if(chunk.memory)
    free(chunk.memory);

  /* we're done with libcurl, so clean it up */
  curl_global_cleanup();

  return 0;
}
           

1.先貼上官方的example.如上

2.多線程中的使用:

在程式最開始的地方 curl_global_init, 然後再建立各個線程,在各個線程中用 curl_easy_init來建立curl 的 handle,然後針對這個handle用 curl_easy_setopt(handle, xxx, xxx) 設定傳輸相關的各種參數,最後用 cur_easy_perform(handle)來執行動作,傳輸結束後,最後調用 curl_easy_cleanup(handle) 來釋放,然後結束線程.在程式的主線程中,等所有線程都結束後,在程式退出前,調用 curl_global_cleanup來結束 libcurl

1. 如果不加限定的取消線程curl相關函數中會有記憶體洩露的問題

    解決方法:設定線程當進入某個函數時該函數不可取消

2. 對于libcurl來說,如果不可取消,将會導緻下載下傳過程一直持續下去,為了避免線程已經取消,而curl函數不退出的情況:

    解決方法:配置curl參數,當下載下傳速度過低時,就放棄繼續執行

3. 連接配接未建立時(即完全無網絡的情況下), 1的解決方法将導緻函數一直執行完才能退出,一般來說是到CURLOPT_TIMEOUT時間才會退出函數

    解決方法:CURLOP_CONNECTTIMEOUT配置連接配接逾時函數,一旦連接配接逾時,就退出函數

3.一些設定

curl_easy_setopt(m_pCurl, CURLOPT_HTTPHEADER, chunk);    

                curl_easy_setopt(m_pCurl, CURLOPT_TIMEOUT, 120);

                curl_easy_setopt(m_pCurl, CURLOPT_URL, pURL);

                curl_easy_setopt(m_pCurl, CURLOPT_WRITEFUNCTION, Url_IconWrite);

                curl_easy_setopt(m_pCurl, CURLOPT_WRITEDATA, &m_HtmlBuff);

繼續閱讀