I recently learned about libcurl and used it to submit a POST request. However, the returned response is always unable to verify whether the POST request has been successfully submitted.
1. First, let's take a look at a successful request submitted based on firebug. Here we take the Xiami I like in login as an example ~
1.1 HTTP interaction of this POST request
1.2 post
1.3 get through server-side redirect
2. OK. Next, use libcurl to send a POST request to Xiami.
2.1 General process of using libcurl
Curl_easy_init ()
Curl_easy_setopt ()
Curl_easy_perform ()
Curl_easy_cleanup ()
Haha ~ Super simple, the specific meaning here is not detailed, see http://curl.haxx.se/libcurl/c/
2.2 let's take a look at the simple code.
C code
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <string. h>
- # Include <curl/curl. h>
- # Define posturl "http://www.xiami.com/member/login"
- # Define postfields "email = myemail@163.com & Password = mypassword & autologin = 1 & submit = login & type ="
- # Define FILENAME "curlposttest. log"
- Size_t write_data (void * buffer, size_t size, size_t nmemb, void * userp );
- Int main (INT argc, char * argv []) {
- Curl * curl;
- Curlcode res;
- File * fptr;
- Struct curl_slist * http_header = NULL;
- If (fptr = fopen (filename, "W") = NULL ){
- Fprintf (stderr, "fopen file error: % s \ n", filename );
- Exit (1 );
- }
- Curl = curl_easy_init ();
- Curl_easy_setopt (curl, curlopt_url, posturl );
- Curl_easy_setopt (curl, curlopt_postfields, postfields );
- Curl_easy_setopt (curl, curlopt_writefunction, write_data );
- Curl_easy_setopt (curl, curlopt_writedata, fptr );
- Curl_easy_setopt (curl, curlopt_post, 1 );
- Curl_easy_setopt (curl, curlopt_verbose, 1 );
- Curl_easy_setopt (curl, curlopt_header, 1 );
- Curl_easy_setopt (curl, curlopt_followlocation, 1 );
- Curl_easy_setopt (curl, curlopt_cookiefile, "/users/Zhu/cprojects/curlposttest. Cookie ");
- Res = curl_easy_perform (curl );
- Curl_easy_cleanup (curl );
- }
- Size_t write_data (void * buffer, size_t size, size_t nmemb, void * userp ){
- File * fptr = (File *) userp;
- Fwrite (buffer, size, nmemb, fptr );
- }
# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <curl/curl. h> # define posturl "http://www.xiami.com/member/login" # define postfields "email = myemail@163.com & Password = mypassword & autologin = 1 & submit = login & type =" # define FILENAME "curlposttest. log "size_t write_data (void * buffer, size_t size, size_t nmemb, void * userp); int main (INT argc, char * argv []) {curl * curl; curlcode res; file * fptr; struct curl_slist * http_header = NULL; If (fptr = fopen (filename, "W") = NULL) {fprintf (stderr, "fopen file error: % s \ n ", filename); exit (1) ;}curl = curl_easy_init (); curl_easy_setopt (curl, curlopt_url, posturl); curl_easy_setopt (curl, curlopt_postfields, postfields ); curl_easy_setopt (curl, scheme, write_data); Scheme (curl, scheme, fptr); Scheme (curl, curlopt_post, 1); curl_easy_setopt (curl, scheme, 1); curl_easy_setopt (curl, curlopt_header, 1); curl_easy_setopt (curl, curlopt_followlocation, 1); curl_easy_setopt (curl, curlopt_cookiefile, "/users/Zhu/cprojects/curlposttest. cookie "); Res = curl_easy_perform (curl); curl_easy_cleanup (curl);} size_t write_data (void * buffer, size_t size, size_t nmemb, void * userp) {file * fptr = (File *) userp; fwrite (buffer, size, nmemb, fptr );}
2.3 let's talk about some operations here.
Curlopt_url: URL address
Curlopt_postfields: Post Parameter
Curlopt_writefunction: the address of the function that operates on the returned data
Curlopt_writedata: Set the fourth value of writefunction.
Curlopt_post: if it is set to non-0, this operation is post.
Curlopt_verbose: Set to non-0 to print request information during execution
Curlopt_header: Set to non-0. Send the response header information along with the response body to writefunction.
Curlopt_followlocation: Set to non-0. Response Header Information location
Curlopt_cookiefile: Haha, this is too important. The reason why I tried many times before I could not verify whether the post was successful is that I didn't set this option. Set the corresponding cookiefile path, which does not necessarily need to exist on the physical disk
2.4 The next step is the result of successful return. Oh, the following zhuzhu can be used as an evidence. Sorry, Xiami has a better name on it ~