Use libcurl in Windows

Source: Internet
Author: User

1. Download and install

Download libcurl -- http://curl.haxx.se/dlwiz/

Tortoise SVN has libsasl Library

 

2. Set Windows Environment Variables

Libcurl_root -- libcurl storage path, such as E:/opensdk/libcurl-7.19.3

 

3. Set vs project properties, add $ (libcurl_root)/include, $ (libcurl_root)/lib, add curllib. lib to the dynamic link, and add curllib_static.lib to the static link.

You need to add the following items to the static link:

# Pragma comment (Lib, "ws2_32.lib ")
# Pragma comment (Lib, "wldap32.lib ")

Curl_staticlib

If you're using libcurl as a Win32 DLL, you must use the curlopt_writefunction if you set curlopt _
Writedata-or you will experience crashes.

 

4. problems that must be considered when using libcurl to operate http:

4.1 is the operation synchronous or asynchronous?

The easy interface is synchronous.

4.2 is thread security?

Never share the same libcurl handle between threads. Whether it is easy handle or multi handle, libcurl is thread-safe. Are other dependent libraries thread-safe?

Can 4.3 be used in high concurrency?

4.4 what kind of License (MIT) is used)

4.5 how to set the HTTP Request Method (post, get or other), header, Protocol version (HTTP/1.0 or HTTP/1.1)

4.6 how to obtain the returned status code, header, Protocol version (HTTP/1.0 or HTTP/1.1)

Curl_easy_getinfo (curl, curlinfo_response_code, & response_code );

Curl_easy_setopt (curl, curlopt_customrequest, "setup ");

Curl_easy_setopt (curl, curlopt_http_version, curl_http_version_1_1 );

4.7

Curlopt_headerfunction, curlopt_headerdata
The prototype of the callback function is size_t function (void * PTR, size_t size, size_t nmemb, void * stream). Once the libcurl receives the HTTP header data, it will call this function. Curlopt_writedata transmits the pointer to libcurl, which indicates the source of the stream pointer of the curlopt_headerfunction.

 

// Register the callback function Curl_easy_setopt (easy_handle, curlopt_readfunction, read_function ); // Set the custom pointer Curl_easy_setopt (easy_handle, curlopt_readdata, & filedata );
 
Curl_easy_setopt (easy_handle, curlopt_upload, 1l); // you can specify the size of the uploaded file.
 
Curl_easy_setopt (easy_handle, curlopt_infilesize_large, file_size );

5. Several important libcurl Functions

Curl_easy_setopt (curl, curlopt_verbose, 1l );

Curl_easy_setopt (curl, curlopt_connecttimeout, 2000 );

Curl_easy_getinfo (curl, curlinfo_response_code, & response_code );

Curl_slist * headers = NULL;

Headers = curl_slist_append (headers, "Content-Type: text/XML ");

Curl_easy_setopt (curl, curlopt_httpheader, headers );

Curl_httppost * post = NULL;

Curl_httppost * Last = NULL;
Curl_formadd (& post, & last, curlform_copyname, "name", curlform_copycontents, "Zheng", curlform_end );

Curl_formadd (& post, & last, curlform_copyname, "project", curlform_copycontents, "curl", curlform_end );

Curl_easy_setopt (curl, curlopt_writefunction, & process_data );
Curl_easy_setopt (curl, curlopt_writedata, null );

Static size_t process_data (void * PTR, size_t size, size_t nmemb, void * PARAM ){
Return size * nmemb;
}

Curl_easy_setopt (easy_handle, curlopt_userpwd ,"User_name: Password");

You can set curlopt_httpget to 1 to return Easy handle to the original state.

Curl_easy_setopt (easy_handle, curlopt_httpget, 1l); each easy handle stores several recently used connections for reuse. The default value is 5. You can use the curlopt_maxconnects attribute to set the number of connections to be saved. If you do not want to reuse the connection, set the curlopt_fresh_connect attribute to 1. In this way, when each request is submitted, libcurl closes the previously created connection and creates a new connection. You can also set curlopt_forbid_reuse to 1, so that the connection will be closed immediately after each request is executed.

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.