Obtain web page and HTTP header information through curl

Source: Internet
Author: User

 

Curl is a powerful HTTP operation library that can be used to perform HTTP operations, reducing the complexity of using socket in code. it also contains a file transfer tool curl that uses the URL syntax to work under the command line. specific can refer to: http://curl.haxx.se/This is the home page of the curl developer.

The following is my example to test the HTTP returned data.

 

# Include <stdio. h> <br/> # include <curl/curl. h> <br/> // page data callback function <br/> static size_t write_callback (void * PTR, size_t size, size_t nmemb, void * Stream) <br/>{< br/> int Len = size * nmemb; <br/> int written = Len; <br/> fwrite (PTR, size, nmemb, (File *) stream); <br/> // written = fputs (PTR, (File *) stream); <br/> return written; <br/>}< br/> // return the HTTP header callback function <br/> static size_t header_callback (void * PTR, size_t size, size_t nmemb, void * Stream) <br/>{< br/> int Len = size * nmemb; <br/> int written = Len; <br/> return written; <br/>}</P> <p> int main (void) <br/>{< br/> curl * curl; <br/> curlcode res; <br/> struct curl_slist * chunk = NULL; </P> <p> curl = curl_easy_init (); <br/> If (curl) {</P> <p> // Add HTTP header Additional Information <br/> chunk = curl_slist_append (chunk, "Accept-language: ZH-CN "); <br/> // chunk = curl_slist_append (chunk, "accept: image/GIF, image/X-xbitmap, image/JPEG, image/pjpeg, application/X-Shockwave-flash, */* "); <br/> chunk = curl_slist_append (chunk," Accept-encoding: gzip, deflate "); <br/> chunk = curl_slist_append (chunk, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; CBA )"); <br/> chunk = curl_slist_append (chunk, "connection: keep-alive"); <br/> // set the debugging mode <br/> curl_easy_setopt (curl, curlopt_verbose, 1l); <br/> curl_easy_setopt (curl, curlopt_url, "192.168.1.25"); <br/> curl_easy_setopt (curl, curlopt_httpheader, Chunk ); </P> <p> // set callback function <br/> curl_easy_setopt (curl, curlopt_headerfunction, header_callback); <br/> curl_easy_setopt (curl, curlopt_writefunction, write_callback ); </P> <p> res = curl_easy_perform (curl); </P> <p>/* always cleanup */<br/> curl_easy_cleanup (curl ); </P> <p> curl_slist_free_all (chunk); <br/> chunk = NULL; <br/>}< br/> return 0; <br/>}< br/>

 

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.