c語言libcurl 使用例項get/post方法+c語言字串處理
阿新 • • 發佈:2019-02-19
#include <stdio.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); } } /*將str1字串中第一次出現的str2字串替換成str3*/ void replaceFirst(char *str1, char *str2, char *str3) { char str4[strlen(str1) + 1]; char *p; strcpy(str4, str1); if ((p = strstr(str1, str2)) != NULL)/*p指向str2在str1中第一次出現的位置*/ { while (str1 != p && str1 != NULL)/*將str1指標移動到p的位置*/ { str1++; } str1[0] = '/0'; /*將str1指標指向的值變成/0,以此來截斷str1,捨棄str2及以後的內容,只保留str2以前的內容*/ strcat(str1, str3); /*在str1後拼接上str3,組成新str1*/ strcat(str1, strstr(str4, str2) + strlen(str2)); /*strstr(str4,str2)是指向str2及以後的內容(包括str2),strstr(str4,str2)+strlen(str2)就是將指標向前移動strlen(str2)位,跳過str2*/ } } /*將str1出現的所有的str2都替換為str3*/ void replace(char *str1, char *str2, char *str3) { while (strstr(str1, str2) != NULL) { replaceFirst(str1, str2, str3); } } /*擷取src字串中,從下標為start開始到end-1(end前面)的字串儲存在dest中(下標從0開始)*/ void substring(char *dest, char *src, int start, int end) { char *p = src; 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] = '/0'; return; } /*返回src中下標為index的字元*/ char charAt(char *src, int index) { char *p = src; int i = 0; if (index < 0 || index > strlen(src)) return 0; while (i < index)i++; return p[i]; } /*返回str2第一次出現在str1中的位置(下表索引),不存在返回-1*/ int indexOf(char *str1, char *str2) { char *p = str1; int i = 0; p = strstr(str1, str2); if (p == NULL) return -1; else { while (str1 != p) { str1++; i++; } } return i; } /*返回str1中最後一次出現str2的位置(下標),不存在返回-1*/ int lastIndexOf(char *str1, char *str2) { char *p = str1; int i = 0, len = strlen(str2); p = strstr(str1, str2); if (p == NULL)return -1; while (p != NULL) { for (; str1 != p; str1++)i++; p = p + len; p = strstr(p, str2); } return i; } /*刪除str左邊第一個非空白字元前面的空白字元(空格符和橫向製表符)*/ void ltrim(char *str) { int i = 0, j, len = strlen(str); while (str[i] != '/0') { if (str[i] != 32 && str[i] != 9)break; /*32:空格,9:橫向製表符*/ i++; } if (i != 0) for (j = 0; j <= len - i; j++) { str[j] = str[j + i]; /*將後面的字元順勢前移,補充刪掉的空白位置*/ } } /*刪除str最後一個非空白字元後面的所有空白字元(空格符和橫向製表符)*/ void rtrim(char *str) { char *p = str; int i = strlen(str) - 1; while (i >= 0) { if (p[i] != 32 && p[i] != 9)break; i--; } str[++i] = '/0'; } /*刪除str兩端的空白字元*/ void trim(char *str) { ltrim(str); rtrim(str); } //這是libcurl接收資料的回撥函式,相當於recv的死迴圈 //其中stream可以自定義資料型別,這裡我傳入的是檔案儲存路徑 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; if (access((char*) stream, 0) == -1) { fp = fopen((char*) stream, "wb"); } else { fp = fopen((char*) stream, "ab"); } if (fp) { fwrite(ptr, size, nmemb, fp); } // printf("%s\n",ptr); fclose(fp); return written; } //加上-lcurl庫 void test_post(char* url,char* data) { CURL *curl; CURLcode res; curl = curl_easy_init(); if (curl) { //www.baidu.com/#wd=java curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); res = curl_easy_perform(curl); curl_easy_cleanup(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 (pos > 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); //指定回撥函式 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); //這個變數可作為接收或傳遞資料的作用 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, CURLINFO_CONTENT_TYPE, &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]); } FILE *fp = NULL; fp = fopen(TMP_FILE, "r"); char src_html[HTML_BUFFER_SIZE]={0}; char output_html[HTML_BUFFER_SIZE]={0}; char tmp_html[HTML_BUFFER_SIZE]={0}; if (fp) { fread(src_html, HTML_BUFFER_SIZE, 1, fp); strcpy(tmp_html,src_html); if(index <0) { GetCharset(tmp_html,tocode); printf("%s\n",tocode); } int iRet; //開啟字符集轉換 iconv_t hIconv = iconv_open(tocode,"iso-8859-1"); if (-1 == (int) hIconv) { return -1; //開啟失敗,可能不支援的字符集 } printf("%s\n",src_html); //開始轉換 iRet = iconv(hIconv, (char**) (&src_html), strlen(src_html), (char**) (&output_html), strlen(src_html)); printf("%s\n", output_html); printf("ok"); if(strcmp(output_html,"")==0) { printf("%s\n",src_html); } //關閉字符集轉換 iconv_close(hIconv); } } curl_easy_cleanup(curl); } } int main(int argc, char* argv) { //printf("%s\n",argv[1]); //http://192.168.1.6:8080/TestServer/index.html?fdasf=123456&af=89 test_post("http://192.168.1.6:8080/TestServer/index.html","wd=hava&hehe=123456"); //test_get("http://www.baidu.com/"); printf("\nok"); return 0; }