libcurl在嵌入式设备C 的使用

来源:互联网 发布:淘宝运动衣 编辑:程序博客网 时间:2024/06/04 21:54

最近用海思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为

<html><body><form action="http://150.10.20.167:8080/TestJspSmartUpload/do_upload.jsp" method="post" enctype="multipart/form-data">  File:<input type="file" name="sendfile" /><br/>   FileName:<input type="text" name="filename" /><br/>   <input type="submit" name="submit" value="Submit" />  </form> </body></html>

C程序代码为:

#include <stdio.h>  #include <string.h>  #include <curl.h>    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\n",                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服务器能收到图像文件。
0 0