Libcurl (1)

Source: Internet
Author: User
Tags bitmask

Recently, I want to download some data from a website and find cURL on the Internet. I just want to learn and get it!

  

CURL's official website is http://curl.haxx.se/, directly download the source code package to compile it, In download contains the source code in, the latest version is curl-7.32.0 (http://curl.haxx.se/download/curl-7.32.0.tar.gz ).

 

Libcurl is an open-source url transmission library that supports FTP, HTTP, and other protocols. I use C language. There are many curl interfaces. Currently, the libcurl-easy interface is powerful enough to meet my requirements. So here we will study the functions in libcurl-easy, others will be used in future studies.

General curl usage process:

 curl_easy_init();         curl_easy_setopt();         curl_easy_perform();         curl_easy_cleanu p();        

Some functions in <culr/curl. h>:

、 CURLcode curl_global_init( flags);

This function must be called before other curl functions are called. Only one call is required, and multiple calls are the same as one call.

This function is non-thread-safe, so it cannot be called in other threads when it is running.

If you do not call the curl_global_init () function before using the curl_easy_init () function, the program automatically calls it.

Parameters:

CURL_GLOBAL_ALL initializes all systems except CURL_GLOBAL_ACK_EINTR.

CURL_GLOBAL_SSL initialize SSL

CURL_GLOBAL_WIN32 initializes Win32 socket libraries.

CURL_GLOBAL_NOTHING does not initialize any system

CURL_GLOBAL_DEFAULT is equivalent to CURL_GLOBAL_ALL.

After the label is set for CURL_GLOBAL_ACK_EINTR, when curl is connected or waiting for data requests, curl will receive the EINTR condition. Otherwise, curl will wait.

Return Value:

0 is returned when the request passes normally. A non-zero value indicates an error.

 

、 curl_global_cleanup();

This function releases resources requested by the curl_global_init () function.

This function must be called after the curl_global_init () function and before closing the curl library.

This function is also non-thread-safe.

 

、CURL *curl_easy_init( );

This function is initialized to generate a curl pointer, which is used as input for other easy functions.

This function must be used with the curl_easy_clean () function.

If you do not call curl_global_init () for initialization before calling this function, the system will automatically call the curl_global_init () function.

However, it is recommended that you do not have to have the system call this function automatically, because you may forget to call curl_global_cleanup.

Therefore, it is best to manually call the curl_global_init () function.

Return Value:

Returns a pointer similar to a FILE. If NULL is returned, an error occurs.

 

、 curl_easy_cleanup(CURL * handle );

Call this function to end a curl easy session. Closes a file generated by curl_easy_init ().Handle.

 

、CURLcode curl_easy_setopt(CURL *handle, CURLoption option, parameter);

Set an action for the "easy" handle.

The curl_easy_setopt () function tells libcurl how to do this. By setting appropriate parameters, you can change the libcurl behavior.

Basically all operations are set through this function.

OptionParameters are a series of action operations.

ParameterDepends onOptionParameter settings.

Return Value:

0 or CURL_ OK indicates success. A non-zero value indicates that an error has occurred.

If you set a behavior that cannot be identified by libcurl, it may be because the libcurl version is too low, CURLE_FAILED_INIT will be returned.

 

、CURLcode curl_easy_perform(CURL * handle);

Execute the session operation.

This function is called after the curl_easy_init () function and all the curl_easy_setopt () functions are set, and all the settings are performed.

Return Value:

If the call succeeds, 0 is returned, and non-zero is returned. If CURLOPT_ERRORBUFFER is set in the curl_easy_setopt () function, a readable error message is displayed in the error buffer when non-zero is returned.

 

、CURL *curl_easy_duphandle(CURL *handle);

Clone a curl session handle.

This function returns a curl copy with all the settings of the previous handle.

The new curl handle must also call the curl_easy_cleanup () function when it is disabled.

New copies do not inherit any status information, no connection, no SSL, and no cookies.

Return Value:

If NULL is returned, the clone operation fails.

 

、 *curl_easy_escape( CURL * curl ,  * url ,  length);

The string specified by the URL encoding.

This program converts the input string into a URL encoded string and returns a new string.

Except for a-z, A-Z, 0-9, '-', '.', '_' or '~ 'Other characters are converted into escape characters.

IfLengthIf the parameter is set to 0, strlen () function compute is automatically called.UrlAs a parameter.

You must use the curl_free () function to release the string memory.

Return Value:

A pointer or NULL pointing to the end of a string indicates that the conversion fails.

 

、 *curl_easy_unescape( CURL * curl ,  * url ,  inlength ,  * outlength);

Decodes a specified string from a URL.

All input URL codes are converted into their corresponding binary codes.

IfInlengthIf the parameter is set to 0, strlen () function compute is automatically called.UrlAs a parameter.

IfOutlengthIf the parameter is not-NULL, the function writes the length of the returned string.

Return Value:

A pointer or NULL pointing to the end of a string indicates that the conversion fails.

 

、CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ...);

Used to obtain curl handle information.

 

、CURLcode curl_easy_pause(CURL *handle ,  bitmask);

Pause or start a connection.

A connection can be used to suspend the connection by calling this function or sending the CURL_READFUNC_PAUSE or CURL_WRITEFUNC_PAUSE returned by the callback function for read or write operations.

The handle parameter points to the session to be paused.

The bitmask parameter sets the new connection status. Optional:

CURLPAUSE_RECV

Stop receiving data. This session will not receive data until it calls the function that has not changed its bit status again.

CURLPAUSE_SEND

Stop sending data. This session will not send data until it calls the function without changing the bit status again.

CURLPAUSE_ALL

Stop sending and receiving sessions.

CURLPAUSE_CONT

Restart sending and receiving sessions.

 

、CURLcode curl_easy_recv( CURL * curl ,  * buffer , size_t buflen , size_t * n);

Receives data through a "easy" connection.

This function receives data through a established connection. It can be used with the curl_easy_send () function to implement custom protocols through the libcurl library.

BufferPoint to the address where you want to store the received data.BuflenIs the maximum value of data that you can obtain from the cache,NThe length of the data you receive.

If you establish a connection by setting CURLOPT_CONNECT_ONLY, The curl_easy_recv () function cannot be called.

Before calling the curl_easy_recv () function, you must ensure that the socket has data readable; otherwise, CURLE_AGAIN is returned.

Call the curl_easy_getinfo () function and set the parameter to CURLINFO_LASTSOCKET to obtain the socket.

You can use the system tool select () to check whether data is readable.

Return Value:

If the operation succeeds, CURLE_ OK is returned, and the obtained data is savedBufferAnd the data size will be storedN.

If it fails, the occupied error code is returned.

If no data is readable, CURLE_AGAIN is returned.

If no socket is available, CURLE_UNSUPPORTED_PROTOCOL is returned.

 

、CURLcode curl_easy_send( CURL * curl , const void * buffer , size_t buflen , size_t * n);

Send data through a "easy" connection.

This function sends arbitrary data through established connections. It can be used with the curl_easy_recv () function to implement custom protocols through the libcurl library.

BufferThe address of the data you want to send.BuflenIs the length of the sent data,NThe length of the data that you send.

If you establish a connection by setting CURLOPT_CONNECT_ONLY, The curl_easy_recv () function cannot be called.

Before calling the curl_easy_send () function, you must ensure that the socket has data readable; otherwise, CURLE_AGAIN is returned.

Call the curl_easy_getinfo () function and set the parameter to CURLINFO_LASTSOCKET to obtain the socket.

You can use the system tool select () to check whether data is readable.

Return Value:

If it succeeds, CURLE_ OK is returned, and the size of the actually sent data will be savedN.

If it fails, the occupied error code is returned.

If no socket is available, CURLE_UNSUPPORTED_PROTOCOL is returned.

 

、 curl_easy_reset(CURL *handle);

Reset a curl handle.

Reinitialize a curl handle to make it the same as when you just called the curl_easy_init () function.

The following information in the handle cannot be changed: active connections, Session ID cache, DNS cache, cookies, and share.

 

、  *curl_easy_strerror(CURLcode errornum);

This function uses the errornum parameter to return the string of the error message that has just occurred.

 

、 curl_free(  * ptr );

Release memory.

 

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.