Use libcurl to submit a POST request

Source: Internet
Author: User

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
  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. # Include <string. h>
  4. # Include <curl/curl. h>
  5. # Define posturl "http://www.xiami.com/member/login"
  6. # Define postfields "email = myemail@163.com & Password = mypassword & autologin = 1 & submit = login & type ="
  7. # Define FILENAME "curlposttest. log"
  8. Size_t write_data (void * buffer, size_t size, size_t nmemb, void * userp );
  9. Int main (INT argc, char * argv []) {
  10. Curl * curl;
  11. Curlcode res;
  12. File * fptr;
  13. Struct curl_slist * http_header = NULL;
  14. If (fptr = fopen (filename, "W") = NULL ){
  15. Fprintf (stderr, "fopen file error: % s \ n", filename );
  16. Exit (1 );
  17. }
  18. Curl = curl_easy_init ();
  19. Curl_easy_setopt (curl, curlopt_url, posturl );
  20. Curl_easy_setopt (curl, curlopt_postfields, postfields );
  21. Curl_easy_setopt (curl, curlopt_writefunction, write_data );
  22. Curl_easy_setopt (curl, curlopt_writedata, fptr );
  23. Curl_easy_setopt (curl, curlopt_post, 1 );
  24. Curl_easy_setopt (curl, curlopt_verbose, 1 );
  25. Curl_easy_setopt (curl, curlopt_header, 1 );
  26. Curl_easy_setopt (curl, curlopt_followlocation, 1 );
  27. Curl_easy_setopt (curl, curlopt_cookiefile, "/users/Zhu/cprojects/curlposttest. Cookie ");
  28. Res = curl_easy_perform (curl );
  29. Curl_easy_cleanup (curl );
  30. }
  31. Size_t write_data (void * buffer, size_t size, size_t nmemb, void * userp ){
  32. File * fptr = (File *) userp;
  33. Fwrite (buffer, size, nmemb, fptr );
  34. }
# 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 ~

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.