在老东家写的最后一个示例了,留个纪念吧。
1.下载libcurl源码并编译libcurl.lib库文件
官网(https://curl.haxx.se/libcurl/)下载源码包,在windows上有对应的vs工程,这也是后来才发现的,工程sln文件projects\Windows目录下,从VC6-VC14都有提供,在打开工程之前,需要在vs的命令行环境下运行上层目录的generate.bat脚本,产生vcproject文件
Linux上就比较简单了,直接configure;make;make install即可
2.简单了解libcurl的easy接口
curl_easy_init(); //初始化一个curl的handle
curl_easy_setopt(); //设置获取的handle的参数,可以指定POST/GET和header等
curl_easy_perform(); //执行curl请求
curl_easy_reset(); //重置handle先前设定的所有参数
curl_easy_cleanup(); //释放handle
最后的代码如下,省去了业务部分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
/** https://curl.haxx.se/libcurl/c/getinmemory.html **/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "curl/curl.h" #define true 1 #define false 0 //MemoryStruct是用来给curl的callback函数WriteMemoryCallback回传数据的,很有用 struct MemoryStruct { char *memory; size_t size; }; //callback函数,用来在curl_eacy_perform之后,通过自己定义的函数 //获取网络上返回的数据,此示例是官方公布,将数据存入内容的方式,写的很巧妙 static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) { size_t realsize = size * nmemb; struct MemoryStruct *mem = (struct MemoryStruct *)userp; mem->memory = (char*)realloc(mem->memory, mem->size + realsize + 1); if (mem->memory == NULL) { /* out of memory! */ printf("not enough memory (realloc returned NULL)\n"); return 0; } memcpy(&(mem->memory[mem->size]), contents, realsize); mem->size += realsize; mem->memory[mem->size] = 0; return realsize; } //字符串替换函数,业务需要,处理chunk返回的数据用 char* str_replace(char *src,char *rep,char *with) { char *index; char *result,*tmp,*next; int count=0,len_front; int len_with=strlen(with); int len_rep=strlen(rep); next=src; for (count = 0; tmp = strstr(next, rep); ++count) { next = tmp + len_rep; } tmp=result=(char*)malloc(strlen(src)+count*(len_rep-len_with) + 1); if (!result) return NULL; while (count--) { index = strstr(src, rep); len_front = index - src; tmp = strncpy(tmp, src, len_front) + len_front; tmp = strcpy(tmp, with) + len_with; src += len_front + len_rep; // move to next "end of rep" } strcpy(tmp, src); return result; } int main(int argc,char *argv[]) { CURL *curl; CURLcode res; struct MemoryStruct chunk; chunk.memory = (char*)malloc(1); /* will be grown as needed by the realloc above */ chunk.size = 0; /* no data at this point */ curl = curl_easy_init(); /* some servers don't like requests that are made without a user-agent field, so we provide one */ //curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); char *url_login="http://192.168.1.1/sample"; char *post_data_login="productKey=3f192170-8bc5-49c4-bcc8-619b42ea1e72"; struct curl_slist header1 = {"application/x-www-form-urlencoded; charset=UTF-8",NULL}; //struct curl_slist header2 = {"Content-Type: text/plain; charset=UTF-8",NULL}; curl_easy_setopt(curl,CURLOPT_URL,url_login); //url地址 //curl_easy_setopt(curl,CURLOPT_RETURNTRANSFER,true); curl_easy_setopt(curl,CURLOPT_POST,true); curl_easy_setopt(curl,CURLOPT_HTTPHEADER, &header1); curl_easy_setopt(curl,CURLOPT_POSTFIELDS, post_data_login); curl_easy_setopt(curl,CURLOPT_COOKIEJAR, "c:\\www\\cookie.txt"); curl_easy_setopt(curl,CURLOPT_COOKIEFILE, "c:\\www\\cookie.txt"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); res = curl_easy_perform(curl); if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); return 0; }else { printf("%s\n",chunk.memory); } curl_easy_cleanup(curl); getchar(); } |
从百度点进来的,支持一下,希望站长您多出一些好文章。