Use curl in Windows to implement HTTP requests

Source: Internet
Author: User
 

1: Download the official library

Address: http://curl.haxx.se/download.html search Win32-msvc, there are two versions of the library below, one is with SSL, one is without SSL. I downloaded both of them:

Http://curl.haxx.se/download/libcurl-7.18.0-win32-msvc.zip without SSL

Http://curl.haxx.se/download/libcurl-7.19.3-win32-ssl-msvc.zip with SSL

2: Extract

Compress the two downloaded zip packages, and save them to the E: \ source directory. The two directories are:

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

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

Directory and the project files including source code, Vc, and compiled DLL and Lib files. You can directly use the DLL without compiling.

3: Visual Studio Environment Settings

Without SSL: tool-> Option-> Project-> VC ++ directory-

By default, the platform is Win32. Select the directory containing the following files and add a new line:

Path to the extracted directory e: \ source \ libcurl-7.18.0-win32-msvc \ directory of the include directory, the full path is:

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

Then select the library file and add a new line:

The path is set to the directory where libcurl is stored. Here I set it to E: \ source \ libcurl-7.18.0-win32-msvc.

If you use an SSL package, replace it with the path E: \ source \ libcurl-7.19.3-win32-ssl-msvc.

4. Create a Win32 project. Set it by default.

Add libcurl. lib to the project's additional dependencies under the installation directory of curl.

5: source code

#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: Others

  • If you need support from other libraries (such as zlib) at http://curl.haxx.se/download.html, you can also find the compiled DLL.
  • There is an article using libcurl in Visual Studio on the Internet describing the steps for downloading, compiling, and setting the Environment project.
  • Http://curl.haxx.se/libcurl/c/example.html has detailed sample code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.