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

來源:互聯網
上載者:User

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 有詳細的例子代碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.