Curl post and get

Source: Internet
Author: User
Tags http post

Iii. Application Instances
1. Why libcurl,
(1) As an HTTP client, you can directly use socket to connect to the server and then perform HTTP parsing on the data, but analyze the protocol header to implement proxy... This is too troublesome.
(2) libcurl is an open source client URL transfer library that supports FTP, ftps, TFTP, HTTP, https, Gopher, telnet, dict, file, and LDAP, and supports windows, UNIX, linux and other platforms are easy to use, and library files occupy less than 200 KB of space.
2. Get and post Methods
The client submits data to the service over HTTP connections in two ways: Get and post.
(1) The get method attaches the data to be transmitted to the end of the website and delivers the data to the server together. It has the advantage of high efficiency; the disadvantage is poor security, no more than 1024 characters in data, and must be a 7-bit ASCII code. This method is often used in queries.
(2) Post processes sent data through http post, which has the advantages of strong security, large data volume, and many characters. The disadvantage is that the efficiency is relatively low; this method is used for editing and modifying.
3. Cookie and session
(1) A cookie is a text string handle sent to the client's browser and saved on the client's hard disk. It can be used to persistently maintain data between sessions of a web site. Cookie on the client.
(2) A session is the period from when a visitor arrives at a specific homepage to when the visitor leaves. Each visitor receives a session separately, allowing multiple users on the site to share information across all pages. The session is on the server.
(3) use cookies in libcurl to save the cookies and use the same cookies for subsequent links.
(3.1) When closing the link, write the cookie to the specified file: curl_easy_setopt (curl, curlopt_cookiejar, "/tmp/cookie.txt ");
(3.2) use the current cookie instead of getting it again: curl_easy_setopt (curl, curlopt_cookiefile, "/tmp/cookie.txt ");
4. Differences between HTTP and https
(1) HTTP is sent in plain text. Anyone can intercept and read the content.
(2) HTTPS is an encrypted transmission protocol, and the content transmitted using it is encrypted. HTTPS is an extension of HTTP, and its security is based on the SSL protocol.
5. base64 encoding
(1) If you want to upload a piece of data that contains a large number of special characters, you need to handle many problems such as escape characters. You can use base64 encoding to convert the data into readable strings, base64 consists of a-Z, A-Z, +/a total of 64 characters.
(2) because the component of base64 has a plus sign, and the plus sign is a conversion character in the URL, the plus sign is converted into a space during the process of transmitting it to the server in get or post mode, therefore, you need to replace the plus sign encoded by base64 with "% 2B" before passing base64, so that it can be sent normally.
6. Description of parameters in the curl_setop () function
The curl_setopt () function sets options for a curl session. The option parameter is the setting you want, and the value is the value given by this option. The values of the following options will be used as long integer (specified in the option parameter)
Code routine
# Include <stdio. h>
# Include <curl/curl. h>
Bool geturl (char * filename)
{
Curl * curl;
Curlcode res;
File * FP;
If (FP = fopen (filename, "W") = NULL) // The returned results are stored in a file.
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"); // proxy
Curl_easy_setopt (curl, curlopt_httpheader, headers); // modify the protocol header.
Curl_easy_setopt (curl, curlopt_url, "http://www.google.com/search? Hl = en & Q = xieyan0811 & btng = Google + SEARCH & AQ = F & OQ = xieyan081 ");
Curl_easy_setopt (curl, curlopt_writedata, FP );
Res = curl_easy_perform (curl); // Execute
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 the cookie file
Curl_easy_setopt (curl, curlopt_postfields, "& logintype = uid & U = xieyan & psw = xxx86"); // specify the post content
Curl_easy_setopt (curl, curlopt_proxy, "10.99.60.20201: 8080 ");
Curl_easy_setopt (curl, curlopt_url, "http://mail.sina.com.cn/cgi-bin/login.cgi"); // specify the 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 ");
}

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.