libcurl在嵌入式设备C 的使用

2019-07-12 15:47发布

最近用海思hi3518E开发了个wifi摄录一体机,现在要用http实现信息推送功能,比如在设备发生报警录像时就会推送一条信息“有异常入侵!”和一张抓拍图像到服务器,然后当客户的手机上网时,就会受到推送的信息。在网上下载了curl的开源库,可以用C实现http功能。 一、curl库的编译 curl的下载地址是http://curl.haxx.se/download.html,详细的编译步骤参考http://curl.haxx.se/docs/install.html。在这里简单说明我的编译步骤: >tar xvzf curl-7.37.0.tar.gz >cd  curl-7.37.0 >./configure --host=arm-none-linux --prefix=/home/hank/http/hisi_http CC=arm-hisiv100nptl-linux-gcc --disable-shared --enable-static --without-libidn --without-ssl --without-librtmp --without-gnutls --without-nss --without-libssh2 --without-zlib --without-winidn --disable-rtsp --disable-ldap --disable-ldaps --disable-ipv6 >make  >make install 其中,CC指定编译器,--prefix=/home/hank/http/hisi_http指定make install时库的存放路径,这里编译成静态库,去掉了其中的一些功能。 二、curl库的使用 下面的例子参考了http://blog.csdn.net/sxwyf248/article/details/7984776 在C程序中模拟要实现的html为
File:
FileName:
C程序代码为: #include #include #include int main(int argc, char *argv[]) { CURL *curl; CURLcode res; struct curl_httppost *formpost=NULL; struct curl_httppost *lastptr=NULL; struct curl_slist *headerlist=NULL; static const char buf[] = "Expect:"; curl_global_init(CURL_GLOBAL_ALL); /* Fill in the file upload field */ curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "sendfile", CURLFORM_FILE, "man.jpg", CURLFORM_END); /* Fill in the filename field */ curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "filename", CURLFORM_COPYCONTENTS, "man.jpg", CURLFORM_END); /* Fill in the submit field too, even if this is rarely needed */ curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "Submit", CURLFORM_END); curl = curl_easy_init(); /* initalize custom header list (stating that Expect: 100-continue is not wanted */ headerlist = curl_slist_append(headerlist, buf); if(curl) { /* what URL that receives this POST */ curl_easy_setopt(curl, CURLOPT_URL, "http://150.10.20.167:8080/TestJspSmartUpload/do_upload.jsp"); if ( (argc == 2) && (!strcmp(argv[1], "noexpectheader")) ) /* only disable 100-continue header if explicitly requested */ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); /* Perform the request, res will get the return code */ res = curl_easy_perform(curl); /* Check for errors */ if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s ", curl_easy_strerror(res)); /* always cleanup */ curl_easy_cleanup(curl); /* then cleanup the formpost chain */ curl_formfree(formpost); /* free slist */ curl_slist_free_all (headerlist); } return 0; } 编译链接:arm-hisiv100nptl-linux-gcc -o upload upload.c -I./include/curl -L./lib -lcurl -lrt

该例子在平台上运行正常,http服务器能收到图像文件。