C + + Libcurl library for HTTP network communication programming

Source: Internet
Author: User
Tags curl error status code http authentication http post

  first, Libcurl BASIC programming Framework
Libcurl is a cross-platform network Protocol library that supports HTTP, HTTPS, FTP, Gopher, Telnet, dict, file, and LDAP protocols. Libcurl also supports HTTPS certificate authorization, HTTP POST, http PUT, FTP upload, HTTP basic form uploads, proxies, cookies, and user authentication. Want to know more about Libcurl introduction, can go to the official website http://curl.haxx.se/to understand, here no longer detailed. Win32 version of LIBCURL:Http://curl.freeby.pctools.cl/download/libcurl-7.18.0-win32-msvc.zip in Libcurl-based programs, the main use of callback function (callback function) to complete the transfer task, the user set a variety of parameters and callback functions before starting the transfer, when the condition is met Libcurl will call the user's callback function to implement a specific function.       Here is the process to complete the transfer task using Libcurl: 1. Call Curl_global_init ()Initialize Libcurl
2. Call Curl_easy_init ()function get easy interface type pointer
3. Call curl_easy_setopt ()Set transfer options
4. According to curl_easy_setopt ()Set the transport options to implement callback functions to complete user-specific tasks
5. Call Curl_easy_perform ()function to complete the transfer task
6. Call Curl_easy_cleanup ()Freeing memory
Set during the whole process curl_easy_setopt (Parameters are the most critical, and almost all of the Libcurl programs use it. second, some basic functions
1.CURLcode Curl_global_init (long flags);
Describe:
This function can only be used once. (In fact, it can still be used after calling the Curl_global_cleanup function)
If this function is not called when the Curl_easy_init function is called, it is called automatically by the Libcurl library. so it's a good idea to call this function in multiple threads to prevent multiple calls when curl_easy_init in the thread. Note: Although Libcurl is thread-safe, curl_global_init is not guaranteed to be thread-safe, so do not call curl_global_init in each thread, and the function's call should be placed in the main thread.
Parameters: Flags
Curl_global_all//Initialize all possible calls.
CURL_GLOBAL_SSL//initialization supports Secure Sockets Layer.
CURL_GLOBAL_WIN32//Initialize Win32 socket font.
Curl_global_nothing//No additional initialization.
2 void Curl_global_cleanup (void);
Description: Used to clean up the work done by Curl_global_init at the end of Libcurl. A function similar to close. Note: Although Libcurl is thread-safe, curl_global_cleanup is not guaranteed to be thread-safe, so do not call curl_global_init in each thread, and the function's call should be placed in the main thread.
3 char *curl_version ();
Description: Prints the version of the current Libcurl library.
4 CURL *curl_easy_init ();
Describe:
Curl_easy_init is used to initialize a pointer to curl (Some like a pointer that returns a file type). The corresponding at the end of the call to use the Curl_easy_cleanup function cleanup.
General Curl_easy_init means the beginning of a conversation. It returns a Easy_handle (Curl* object), which is typically used in functions of the easy series.
5 void Curl_easy_cleanup (Curl *handle);
Describe:
This call is used to end a session. With Curl_easy_init.
Parameters:
A pointer to the curl type.
6 Curlcode curl_easy_setopt (Curl *handle, curloption option, parameter);
Description: This function is most important. Almost all curl programs use it frequently. It tells the Curl library how the program will behave. For example, to view the HTML code of a Web page. (This function is somewhat like the IOCTL function) parameter:
1 pointer to curl type
2 options for various curloption types. (both are defined in the Curl.h library and can be viewed by man)
3 parameter This parameter can be either a pointer to a function, a pointer to an object, or a long variable. What it does with it depends on the second parameter.
Curloption This parameter takes a lot of value. The manual can be viewed in detail.
7 Curlcode curl_easy_perform (Curl *handle);Description: This function is called after initializing a pointer of type curl and curl_easy_setopt completion. As the literal meaning says, perform is like a stage. Let's set the
option to work. Parameters:
A pointer to the curl type.
three, the Curl_easy_setopt function partial option Introduction
This section mainly describes HTTP-related parameters in curl_easy_setopt. This function is a very important function in curl, all of Curl's settings are done in this function, the function has a number of settings options, note that this section is only a few common options described. 1. Curlopt_url
Set Access URL
2. Curlopt_writefunction,curlopt_writedata
The callback function prototype is: size_t function (void *ptr, size_t size, size_t nmemb, void *stream);The function will be called after the Libcurl receives the data, so the function does more data saving functions, such as processing the download file. Curlopt_writedata is used to indicate the source of the stream pointer in the Curlopt_writefunction function. If you do not set the callback function to easy handle through the Curlopt_writefunction property, Libcurl will provide a default callback function that simply prints the received data to standard output. You can also pass an open file pointer to the default callback function by using the Curlopt_writedata property to output the data to a file.
3. Curlopt_headerfunction,curlopt_headerdata
The callback function prototype is size_t function (void *ptr, size_t size,size_t nmemb, void *stream); Libcurl This function is called once the HTTP header data has been received. Curlopt_writedata passes the pointer to Libcurl, which indicates the source of the stream pointer to the Curlopt_headerfunction function.
4. Curlopt_readfunction Curlopt_readdata
Libcurl the function that the curlopt_readfunction specifies is called when the read data is required to be passed to the remote host: size_t function (void *ptr, size_t size, size_t nmemb,void * Stream). Curlopt_readdata indicates the stream pointer source in the Curlopt_readfunction function prototype.
5. Curlopt_noprogress,curlopt_progressfunction,curlopt_progressdata
Parameters related to the progress of the data transfer. Curlopt_progressfunction The specified function is normally called once per second by Libcurl, in order for the curlopt_progressfunction to be called, the curlopt_noprogress must be set to False, Curlopt_progressdata The specified parameter will be the first parameter of the specified function as Curlopt_progressfunction
6. Curlopt_timeout,curlopt_connectiontimeout:
Curlopt_timeout Curlopt_connectiontimeout Set connection wait time due to setting transfer time
7. Curlopt_followlocation
Set the relocation URL
8. Curlopt_range:curlopt_resume_from:
The breakpoint continues to pass related settings. Curlopt_range specifies that the char * parameter is passed to Libcurl, which indicates the RANGE header field of the HTTP domain, for example:
Represents the first 500 bytes: bytes=0-499
Represents a second 500 byte: bytes=500-999
Represents the last 500 bytes: bytes=-500
Represents the range after 500 bytes: bytes=500-
First and last byte: Bytes=0-0,-1
Specify several ranges at the same time: bytes=500-600,601-999
Curlopt_resume_from passes a long argument to Libcurl, specifying the offset you want to start passing.
Iv. Description of the Curl_easy_perform function (Error status code)
This function is to complete all the options specified by curl_easy_setopt, and this section focuses on the return value of Curl_easy_perform. Returning 0 means everything OK, and non-0 represents an error occurrence. Main error code Description:
1. Curle_ok
Mission accomplished, everything's fine.
2 Curle_unsupported_protocol
Unsupported protocol, specified by the header of the URL
3 Curle_couldnt_connect
Cannot connect to remote host or proxy
4 curle_remote_access_denied
Access is denied
5 Curle_http_returned_error
HTTP return error
6 Curle_read_error
Read local file error to get a detailed error description string, you can const char *curl_easy_strerror (Curlcode errornum)This function is obtained.

v. HTTP message headers used by Libcurl
When you send an HTTP request using Libcurl, it automatically adds some HTTP headers. We can manually replace, add, or delete the corresponding HTTP message headers through the Curlopt_httpheader property.
Host
The http1.1 (most http1.0) versions require the client to request this information header.
Pragma
"No-cache". Indicates that the data is not buffered.
Accept
"*/*"。 Indicates that any type of data is allowed to be received.
Expect
When a request is submitted to the HTTP server as a post, Libcurl sets the message header to "100-continue", which requires the server to return an "OK" message before the request is formally processed. If the data for the post is small, libcurl may not set the message header.
Custom Options
More and more protocols are now built on top of the HTTP protocol (e.g. SOAP), thanks largely to HTTP reliability and the widespread use of proxy support (which can penetrate most firewalls). These protocols can be used in a very different way than traditional HTTP. In this respect, libcurl a good support.
Custom Request mode (Customrequest)
HTTP supports get, head or post submission requests. You can set Curlopt_customrequest to set the custom request method, and Libcurl submit the request by default in Get mode:
curl_easy_setopt (Easy_handle, Curlopt_customrequest, "myownrequest");

Modify message Headers

The HTTP protocol provides a message header that tells the server how to handle the request, and the response message header tells the browser how to process the received data. In Libcurl, you are free to add these message headers:

struct Curl_slist *headers=null; /* init to NULL is important */headers = Curl_slist_append (headers, "Hey-server-hey:how am You?"); headers = curl_slist_append (headers, "X-silly-content:yes");/* Pass our list of custom made headers */curl_easy_setopt (ea Syhandle, Curlopt_httpheader, headers); Curl_easy_perform (Easyhandle); /* Transfer HTTP */curl_slist_free_all (headers); /* Free the header list */

For a message header that already exists, you can reset its value:

Delete message headers
For a message header that already exists, set its contents to NULL, and Libcurl will not commit the message header at the same time as the request is sent:

headers = curl_slist_append (Headers, "Accept:");

Vi. getting HTTP reply header information

When an HTTP request is made, the server returns the answer header and answer data, and if it is only the print answer header, it can be done directly through the curl_easy_setopt (Curl, curlopt_headerfunction, print function). Here you need to get the specific information in the answer header, such as the answer code, cookies list, etc., you need to pass the following function:
Curlcode curl_easy_getinfo (Curl *curl, curlinfo info, ...);
The info parameter is what we need to get, here are some parameter values:
1.curlinfo_response_code
Get the answer code
2.curlinfo_header_size
Head size
3.curlinfo_cookielist
Cookies List

In addition to obtaining response information, this function can also get some internal information about curl, such as request time, connection time, and so on.

More parameters can refer to the API documentation.

Vii. Multithreading Issues
The first basic principle is that you should never share the same libcurl handle (CURL * object) between threads, whether it's easy handle or multi handle (this article only describes Easy_handle). A thread can only use one handle at a time.
Libcurl is thread-safe, with two exceptions: signal (signals) and SSL/TLS handler. The signal is used for time-out invalidation name resolution (timing out name resolves). Libcurl relies on other libraries to support SSL/STL, so when accessing HTTPS or FTPs URLs in a multithreaded manner, these libraries should meet some of the requirements for multithreaded operations. For details, refer to:
Openssl:http://www.openssl.org/docs/crypto/threads.html#description

Gnutls:http://www.gnu.org/software/gnutls/manual/html_node/multi_002dthreaded-applications.html

NSS: Claims to be multithreaded security.

Eight, when Libcurl not work properly
There is always a reason for transmission failure. You may have incorrectly set some Libcurl properties or do not correctly understand the meaning of certain properties, or the remote host will return some content that cannot be parsed correctly.
Here's a golden rule to deal with these problems: Setting the Curlopt_verbose property to 1,libcurl will output some of the details of the communication process. If the HTTP protocol is used, the request Header/response header is also output. Set Curlopt_header to 1, and the header information will appear in the contents of the message.
Admittedly, there are still bugs in Libcurl.
The more you know about the protocols involved, the less likely you are to make mistakes when using Libcurl.

Nine, about the password
When a client sends a request to the server, many protocols require a user name and password. Libcurl provides several ways to set them up.
Some protocols support specifying the user name and password directly in the URL, similar to the following: Protocol://user:[email protected]/path/. Libcurl can correctly identify the user name and password in this URL and perform the appropriate action. If you provide a special character in the user name and password, you should first URL-encode it.
You can also set the user name and password by using the Curlopt_userpwd property. The parameter is a string formatted as "User:password":
curl_easy_setopt (Easy_handle, Curlopt_userpwd, "User_name:password");
Sometimes when accessing a proxy server, you may be asked to provide a user name and password for user authentication. In this case, Libcurl provides another property curlopt_proxyuserpwd:
curl_easy_setopt (Easy_handle, Curlopt_proxyuserpwd, "User_name:password");
Under the UNIX platform, the user name and password to access FTP may be saved in the $home/.netrc file. Libcurl supports obtaining the user name and password directly from this file:
curl_easy_setopt (Easy_handle, CURLOPT_NETRC, 1L);
When using SSL, you may need to provide a private key for data-safe transport and to set the private key through CURLOPT_KEYPASSWD:
curl_easy_setopt (Easy_handle, curlopt_keypasswd, "Keypassword");

10, HTTP authentication
    When using the HTTP protocol, there are many ways for clients to provide authentication information to the server. The default HTTP authentication method is "Basic", which stores the user name and password in plaintext, BASE64 encoded in the HTTP request header, and sends it to the server. Of course it's not very safe.
    The current version of Libcurl supports validation methods such as: Basic, Digest, NTLM, Negotiate, Gss-negotiate, and SPNEGO. (Translator sigh: Engage in the web for so many years, altogether do not know these HTTP authentication way, really ashamed. You can use the Curlopt_httpauth property to set specific authentication methods:
     curl_easy_setopt (Easy_handle, Curlopt_httpauth, Curlauth_digest);
    When sending authentication information to a proxy server, you can set the authentication method through Curlopt_proxyauth:
     curl_easy_setopt (Easy_ Handle, Curlopt_proxyauth, CURLAUTH_NTLM);
    can also set multiple authentication methods (by bitwise AND), using ' Curlauth_any ' will allow Libcurl to select any authentication method it supports. With multiple authentication methods set by the Curlopt_httpauth or Curlopt_proxyauth property, Libcurl chooses at run time what it considers to be the best way to communicate with the server:
      Curl_easy_setopt (Easy_handle, Curlopt_httpauth, curlauth_digest| Curlauth_basic);  
   //Curl_easy_setopt (Easy_handle, Curlopt_httpauth, Curlauth_any);

PS: Running process may encounter a variety of problems, such as can not find the. dll, can not hit files, etc., please Google's own .... Source: http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html http://dewei.iteye.com/blog/1572016

C + + Libcurl library for HTTP network communication programming

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.