The Linux Curl API uses the C language

Source: Internet
Author: User


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/87/F6/wKioL1flKQ7w2SiCAAAkxuwjTUQ889.gif "title=" Curl.gif "alt=" Wkiol1flkq7w2sicaaakxuwjtuq889.gif "/>


" This article declined to reprint, Original from http://990487026.blog.51cto.com"


Chunli ~$ cat main.c  #include  <stdio.h>   #include  <stdlib.h># include <unistd.h> #include  <curl/curl.h>   #include  <string.h>    #include  <ctype.h>   #include  <iconv.h>   #define  tmp_file   "tmp.html"    #define  html_buffer_size 1024*800  void split (char * *arr, char *str, const char *del)  {  char *s = strtok ( Str, del);  while  (s != null)  {  *arr++ = s;   s = strtok (Null, del);   }  }  /* Replace the first occurrence of the STR2 string in the str1 string with Str3*/  void replacefirst (char *str1, char *str2,  CHAR&NBSP;*STR3)  {  char str4[strlen (str1)  + 1];  char *p;   strcpy (STR4,&NBSP;STR1);   if&nbsp, ((P = strstr (STR1,&NBSP;STR2))  != null)/*p point to str2 where it first appeared in Str1 */ {   while  (str1 != p && str1 != null)/* Move the str1 pointer to the position of P */ {   str1++;  }  str1[0] =  ' + ';  /* the value pointed to by the STR1 pointer to/0, to truncate STR1, discard str2 and future content, Keep only str2 previous content */  strcat (STR1,&NBSP;STR3);  /* str1 after Str3, forming a new Str1*/  strcat (STR1,  strstr (STR4,&NBSP;STR2)  + strlen (str2))  /*strstr (STR4,STR2) is a point of str2 and later (including STR2), Strstr ( STR4,STR2) +strlen (STR2) is to move the pointer forward strlen (STR2) bit, skipping str2*/  }  }  /* Replace all str2 that appear in str1 with Str3*/  void replace (char *str1, char *str2, char * STR3)  {  while  (strstr (STR1,&NBSP;STR2)  != null)  {  replacefirst ( STR1,&NBSP;STR2,&NBSP;STR3);   }  }  /* intercept src string, from subscript to start to end-1 (end front) The string is saved in Dest (subscript starting from 0) */  void SUBSTRING (char *dest, char *src, int start, int end)  {  int  i = start;  if  (Start > strlen (SRC)) return;  if  ( End > strlen (SRC))   end = strlen (SRC);  while  (i <  end)  {  dest[i - start] = src[i];  i++;  }   dest[i - start] =  '   return;  }  /* '; Returns the character */  char charat (Char *src, int index) for index in SRC  {  char  *p = src;  int i = 0;  if  (index < 0  | |  index > strlen (SRC))   return 0;  while  (i <  Index) i++;  return p[i];  }  /* returns the location of the first occurrence of str2 in str1 (the following table index), and there is no return -1*/   int indexof (CHAR&NBSP;*STR1,&NBSP;CHAR&NBSP;*STR2)  {  char *p = str1;  int i = 0;   p = strstr (STR1,&NBSP;STR2);  if  (p == null)   return  -1;  else {  while  (str1 != p)  {  str1++;   i++;  }  }  return i;  }  /* returns the location of the last occurrence of str2 in str1 ( Subscript), there is no return -1*/  int lastindexof (CHAR&NBSP;*STR1,&NBSP;CHAR&NBSP;*STR2)  {  char  *p = str1;  int i = 0, len = strlen (STR2);   P = strstr (STR1,&NBSP;STR2);  if  (p == null) return -1;   while  (P != null)  {  for  (;  str1 != p; str1++) i++;   p = p + len;  p = strstr (P,&NBSP;STR2);  }   return i;  }   /* Delete the white space character before the first non-whitespace character on the left of Str (whitespace and Transverse tab) */  void ltrim (CHAR&NBSP;*STR)  {   int i = 0, j, len = strlen (str);  while  (str[i] ! =  ' + ')  {  if  (str[i] != 32 && str[i] != 9) BREAK;&NBSP;/*32: space, 9: Transverse tab */  i++;  }  if  (i != 0)    for  (j = 0; j <= len - i; j++)  {  str[j] =  str[j + i]; /* the following characters to move forward, supplementing the deleted blank position */  }  }  /* Remove all whitespace characters (spaces and transverse tabs) after the last non-whitespace character of Str */  void rtrim (CHAR&NBSP;*STR)  {  char *p  = str;  int i = strlen (str)  - 1;  while  (i  >= 0)  {  if  (p[i] != 32 && p[i] != 9) break;   i--;  }  str[++i] =  ' + ';   }  /* delete whitespace characters at both ends of str */  void trim (char &NBSP;*STR)  {  ltrim (str);   rtrim (str)   }  //This is the callback function that Libcurl receives the data, Equivalent to Recv's dead loop   //where stream can customize the data type, where I pass in the file save path   static size_t write_callback ( Void *ptr, size_t size, size_t nmemb, void *stream)  {  int  len = size * nmemb;  int written = len;  file  *fp = NULL;  const char *pathfile =  (const char*) stream;if   (Access (pathfile,0)  == -1)  {  fp = fopen ((char*)  stream,  " WB "),   } else {  fp = fopen ((char*)  stream, " AB ");   }  if  (FP)  {  fwrite (PTR,&NBSP;SIZE,&NBSP;NMEMB,&NBSP;FP);   }   // printf ("%S\n ", PTR);   fclose (FP);   return written;  }  //Plus-lcurl library    void test_post (Char* url,char* data)  {  curl *curl;  curl  = curl_easy_init ();  if  (Curl)  {  //www.baidu.com/#wd =java   curl_easy_setopt (Curl, curlopt_url, url);   curl_easy_setopt (Curl, CURLOPT_POST, &NBSP;1L);   curl_easy_setopt (curl, curlopt_postfields, data);   curl_easy_perform (curl);   curl_easy_cleanup (curl);   }  fclose (curl);}   int file_exists (Char *filename)  {  return  (Access (filename, 0)  == 0);   }  int getcharset (Char *src_html,char *charcode)  {   char tmp_html[html_buffer_size]={0};  int pos = indexof (src_html,   "text/html; charset=");   if (p os > 0)  {  strncpy (Tmp_html, src_html + pos + strlen (" Text/html; charset= "),  strlen (src_html)  - pos);   pos = indexof (Tmp_ html,  "\" ");  if  (pos > 0)  {  strncpy (charCode, tmp_html,  pos);   }  }  return 0;  }  void test_get ( Char* url)  {  CURL *curl;  CURLcode res;  curl =  Curl_easy_init ();  if  (Curl)  {  if  (file_exists (tmp_file))    Remove (tmp_file);   curl_easy_setopt (curl, curlopt_url, url);   //Specify callback function    curl_easy_setopt (curl, curlopt_writefunction, write_callback);   //This variable can be used as a function of receiving or transmitting data   curl_easy_setopt (curl, curlopt_writedata, tmp_file);   res = curl_ Easy_perform (curl);   char tocode[64] =  "UTF-8";  if  (curle_ok == res)  {   Char *ct;  res = curl_easy_getinfo (CURL,&NBSP;CURLINFO_CONTENT_TYPE,&NBSP;&AMP;CT);   if  ((curle_ok == res)  && ct)   printf ("we received  content-type: %s\n ",  ct);   //printf (" ====\n ");   //int pos=strcspn (CT, "UTF-8");    int index = indexof (ct,  "=");  char* arr[3];   if  (index > 0)  {  split (arr, ct,  "=");   // printf ("%s\n",  arr[1]);   strcpy (tocode, arr[1]); &NBSP;&NBSP;}&NBSP;&NBSP;FILE&NBSP;*FP  = null;  fp = fopen (tmp_file,  "R");   char src_html[html_ Buffer_size]={0};  char *src_html_p = src_html;size_t srclen = strlen ( src_html); char output_htMl[html_buffer_size]={0};  char *output_html_p = output_html;size_t  outlen  = 0;  char tmp_html[HTML_BUFFER_SIZE]={0};  if  (FP)  {   fread (SRC_HTML,&NBSP;HTML_BUFFER_SIZE,&NBSP;1,&NBSP;FP);      strcpy (tmp_html, src_html)   if (index <0)  {  getcharset (Tmp_html,tocode);   printf ("% S\n ", tocode);   }  //Open Character set conversion     iconv_t hiconv = iconv_ Open (Tocode, "iso-8859-1");  if  ( -1 ==  (ssize_t) hiconv)  {  return   //Open failed, may not support character set     }       printf ("%s\n", src_ HTML);   //start converting     //size_t iconv (iconv_t cd,char **inbuf,  Size_t *inbytesleft,char **outbuf, size_t *outbytesleft); Size_t ret = iconv (Hiconv,&src_html_P,&srclen,&output_html_p,&outlen);                   if (ret == -1) {printf ("Conversion failed \ n"); exit (2);} printf ("%s\n",  output_html);     //printf ("OK");   if (strcmp (output_html, "" ) ==0)   {  printf ("%s\n", src_html);   }  //turn off character set conversion      iconv_close (Hiconv);   }  }  curl_easy_cleanup (Curl);  }   }  int main (INT&NBSP;ARGC,&NBSP;CHAR**&NBSP;ARGV)  {  //test_post ("https:// www.baidu.com/"," ");   test_get (" http://www.baidu.com/");   return 0;  } Chunli Soul Bucket Luo ~$




Compile run:

Chunli Soul Bucket Luo ~$ gcc main.c -wall  -l curl && ./a.outwe  received content-type: text/htmlutf-8<! doctype html><!--status ok-->





" This article declined to reprint, Original from http://990487026.blog.51cto.com"


This article from "Soul Bucket Luo" blog, declined reprint!

Linux Curl API uses C language

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.