windows平臺中使用curl實現Http請求

1: 下載官方庫

 地址:http://curl.haxx.se/download.html  搜索  Win32 - MSVC,下面有兩個版本的庫,一個是帶ssl的,一個是不帶ssl的。我把兩個都下載了下來:

不帶ssl的:http://curl.haxx.se/download/libcurl-7.18.0-win32-msvc.zip

帶ssl的:http://curl.haxx.se/download/libcurl-7.19.3-win32-ssl-msvc.zip

2: 解壓縮

把下載後的兩個zip包分別加壓縮,我這裏保存到E:\source目錄下面,兩個目錄分別是:

E:\source\libcurl-7.18.0-win32-msvc

E:\source\libcurl-7.19.3-win32-ssl-msvc

目錄下以及包括源代碼,VC的工程文件和編譯好的dll、lib文件。 可以直接使用dll無需編譯。

3: Visual studio 環境設置

不帶ssl的:工具-》選項-》項目-》VC++目錄-》

平臺默認是win32,選擇顯示以下文件的目錄-》包含文件,添加新行:

路徑選擇爲剛纔解壓縮的目錄E:\source\libcurl-7.18.0-win32-msvc\目錄下的include目錄,全路徑爲:

E:\source\libcurl-7.18.0-win32-msvc\include

再選擇庫文件,添加新行:

路徑設置爲libcurl的存放目錄,我這裏設置爲E:\source\libcurl-7.18.0-win32-msvc。

如果使用ssl的包的話,那只需要替換爲路徑E:\source\libcurl-7.19.3-win32-ssl-msvc即可

4 新建win32項目.默認設置即可。

將curl的安裝目錄下,libcurl.lib增加到工程的Additional Dependencies中。

5: 源代碼

#include <stdio.h> 
#include <string.h> 
#include <curl/curl.h> 
 
#define MAX_BUF      65536 
 
char wr_buf[MAX_BUF+1]; 
int  wr_index; 
 
/*
 * Write data callback function (called within the context of
 * curl_easy_perform.
 */ 
size_t write_data( void *buffer, size_t size, size_t nmemb, void *userp ) 
{ 
    int segsize = size * nmemb; 
 
    /* Check to see if this data exceeds the size of our buffer. If so,
     * set the user-defined context value and return 0 to indicate a
     * problem to curl.
     */ 
    if ( wr_index + segsize > MAX_BUF ) { 
        *(int *)userp = 1; 
        return 0; 
    } 
 
    /* Copy the data from the curl buffer into our buffer */ 
    memcpy( (void *)&wr_buf[wr_index], buffer, (size_t)segsize ); 
 
    /* Update the write index */ 
    wr_index += segsize; 
 
    /* Null terminate the buffer */ 
    wr_buf[wr_index] = 0; 
 
    /* Return the number of bytes received, indicating to curl that all is okay */ 
    return segsize; 
} 
 
 
/*
 * Simple curl application to read the index.html file from a Web site.
 */ 
int main( void ) 
{ 
    CURL *curl; 
    CURLcode ret; 
    int  wr_error; 
 
    wr_error = 0; 
    wr_index = 0; 
 
    /* First step, init curl */ 
    curl = curl_easy_init(); 
    if (!curl) { 
        printf("couldn't init curl "); 
        return 0; 
    } 
 
    /* Tell curl the URL of the file we're going to retrieve */ 
    curl_easy_setopt( curl, CURLOPT_URL, "www.baidu.com" ); 
 
    /* Tell curl that we'll receive data to the function write_data, and
     * also provide it with a context pointer for our error return.
     */ 
    curl_easy_setopt( curl, CURLOPT_WRITEDATA, (void *)&wr_error ); 
    curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, write_data ); 
 
    /* Allow curl to perform the action */ 
    ret = curl_easy_perform( curl ); 
 
    printf( "ret = %d (write_error = %d) ", ret, wr_error ); 
 
    /* Emit the page if curl indicates that no errors occurred */ 
    if ( ret == 0 ) printf( "%s ", wr_buf ); 
 
    curl_easy_cleanup( curl ); 
 
    return 0; 
} 



6:其他

  • 運行時如果需要其他的庫的支持(例如zlib等)在http://curl.haxx.se/download.html也可以找到,而且也有編譯好的dll
  • 網上有一篇《Using libcurl in Visual Studio》描述了下載、編譯、環境設置項目的步驟
  • http://curl.haxx.se/libcurl/c/example.html 有詳細的例子代碼




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章