First, the concept
1. Why Use Libcurl
1) as an HTTP client, you can directly connect to the server with the socket, and then to the data for HTTP parsing, but to analyze the protocol header, implementation agent ... It's too much trouble.
2) Libcurl is an open source client URL Transfer library that supports Ftp,ftps,tftp,http,https,gopher,telnet,dict,file and LDAP, supports platforms such as Windows,unix,linux, easy to use, and the library file occupies less than 200K of space
2. Get and Post methods
The way the client submits data to the service in HTTP connection is divided into get and post two kinds
1) Get the data to be transmitted after the URL, and then sent to the server together, it has the advantage of high efficiency, the disadvantage is that the security is poor, the data is not more than 1024 characters, must be 7-bit ASCII encoding; This method is often used in queries.
2) post via HTTP POST processing to send data, it has the advantage of strong security, support large amount of data, support word than characters, the disadvantage is relatively low efficiency; Use this method when editing changes.
3. Cookies and Session
1) Cookies
A cookie is a string handle that is sent to the client's browser and stored on the client's hard disk and can be used to persist data between sessions of a Web site. The cookie is on the client.
2) session
A session is the time when a visitor arrives from a specific home page until it leaves. Each visitor will be given a single session to share information across all pages among multiple users of the site. Session is on the server.
3) Use of cookies in Libcurl
Save cookies so that subsequent links use the same cookie as this link
A) The cookie is written to the specified file when the link is closed
Curl_easy_setopt (Curl, Curlopt_cookiejar, "/tmp/cookie.txt");
b) Access to the existing cookie without re-obtaining the cookie
Curl_easy_setopt (Curl, Curlopt_cookiefile, "/tmp/cookie.txt");
b) The difference between HTTP and HTTPS
1) HTTP is sent in plaintext, and anyone can intercept and read the content
2) HTTPS is an encrypted transport protocol, the content transmitted with it is encrypted, HTTPS is an extension of HTTP, its security is based on the SSL protocol
c) Base64 encoding
1) Why to use base64 encoding
If you want to pass a piece of data that contains more special characters, the direct upload will need to deal with a lot of problems such as the Base64 encoding, which can turn data into a readable string, base64 by A-Z, A-Z, +/total 64 characters.
2) Considerations for transmitting Base64 codes
Because the Base64 component has the plus sign, and the plus sign is the URL of the meaning of the characters, so whether it is get or post, to the server process, the plus will be turned into a space, so in the Base64 before you need to Base64 code after the plus sign replaced with "+", This will send it normally.
Second, the routine
d) Code
#include <stdio.h>
#include <curl/curl.h>
BOOL GetUrl (char *filename)
{
CURL *curl;
Curlcode Res;
FILE *FP;
if (fp = fopen (filename, "w")) = = = NULL)//return result with file storage
return false;
struct Curl_slist *headers = NULL;
headers = curl_slist_append (headers, "Accept:agent-007″");
Curl = Curl_easy_init (); Initialization
if (Curl)
{
Curl_easy_setopt (Curl, Curlopt_proxy, "10.99.60.201:8080″);//Agent
Curl_easy_setopt (Curl, Curlopt_httpheader, headers);//Change Protocol header
Curl_easy_setopt (Curl, Curlopt_url, "Http://www.google.com/search?hl=en&q=xieyan0811&btnG=Google+Sea Rch&aq=f&oq=xieyan081″);
Curl_easy_setopt (Curl, Curlopt_writedata, FP);
res = curl_easy_perform (curl); Perform
Curl_slist_free_all (headers);
Curl_easy_cleanup (curl);
}
Fclose (FP);
return true;
}
BOOL PostURL (char *filename)
{
CURL *curl;
Curlcode Res;
FILE *FP;
if (fp = fopen (filename, "w")) = = = NULL)
return false;
Curl = Curl_easy_init ();
if (Curl)
{
Curl_easy_setopt (Curl, Curlopt_cookiefile, "/tmp/cookie.txt"); Specify Cookie File
Curl_easy_setopt (Curl, Curlopt_cookiejar, "/tmp/cookie.txt");
Curl_easy_setopt (Curl, Curlopt_postfields, "&logintype=uid&u=xieyan&psw=xxx86″"); Specify post Content
Curl_easy_setopt (Curl, Curlopt_proxy, "10.99.60.201:8080″");
Curl_easy_setopt (Curl, Curlopt_url, "http://mail.sina.com.cn/cgi-bin/login.cgi"); Specify URL
Curl_easy_setopt (Curl, Curlopt_writedata, FP);
res = curl_easy_perform (curl);
Curl_easy_cleanup (curl);
}
Fclose (FP);
return true;
}
int main (void)
{
GETURL ("/tmp/get.html");
PostURL ("/tmp/post.html");
}
Libcurl get post http