Libcurl reads Web page data into memory

Source: Internet
Author: User
Tags goto

Read the Web page data into memory through Libcurl for further processing.

Key code:

BOOL readtocontentbuff (const char* URL, string& content) {curl* conn = NULL;
  Curlcode code = Curle_failed_init;
  Char Errorbuffer[curl_error_size];
  conn = Curl_easy_init ();
  if (conn = = NULL) {goto exit_code_readtocontentbuff;
  }//code = CURL_EASY_SETOPT (conn, Curlopt_url, URL);
  If (code!= CURLE_OK) {goto exit_code_readtocontentbuff;
  }//code = CURL_EASY_SETOPT (conn, Curlopt_errorbuffer, Errorbuffer);
  If (code!= CURLE_OK) {goto exit_code_readtocontentbuff;
  }//code = CURL_EASY_SETOPT (conn, curlopt_followlocation, 1);
  If (code!= CURLE_OK) {goto exit_code_readtocontentbuff;
  }//code = CURL_EASY_SETOPT (conn, curlopt_writefunction, writer);
  If (code!= CURLE_OK) {goto exit_code_readtocontentbuff;
  }//code = CURL_EASY_SETOPT (conn, curlopt_writedata, &content);
  If (code!= CURLE_OK) {goto exit_code_readtocontentbuff;
  }//code = CURL_EASY_PERFORM (conn);
 If (code!= CURLE_OK) {   Goto Exit_code_readtocontentbuff;
  }//EXIT_CODE_READTOCONTENTBUFF:IF (conn) {CURL_EASY_CLEANUP (conn);
return (Code = = CURLE_OK); }



The specific code for the Witer function is as follows:

int writer (char *data, size_t size, size_t nmemb, String *writerdata) {
  unsigned long sizes = size * NMEMB;
  if (Writerdata = = NULL) {return
	  0;
  }
  Writerdata->append (data, sizes);
  return sizes;
}

Mylaf

2017-08-10 2010



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.