Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30995218
  • 博文数量: 230
  • 博客积分: 2868
  • 博客等级: 少校
  • 技术积分: 2223
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-08 21:48
个人简介

Live & Learn

文章分类

全部博文(230)

文章存档

2022年(2)

2019年(5)

2018年(15)

2017年(42)

2016年(24)

2015年(13)

2014年(1)

2012年(5)

2011年(58)

2010年(56)

2009年(9)

我的朋友

分类: LINUX

2017-11-24 16:03:36

libcurl是免费的轻量级的客户端网络库,支持DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS,POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet, TFTP.支持SSL, HTTPPOST,HTTPPUT, FTP上传, HTTP form上传,代理,cookies, 用户名与密码认证。

系统环境:Ubuntu 14.04.3 LTS
源码:curl-7.49.0.tar.gz (https://curl.haxx.se/download.html)
交叉编译环境:arm-none-linux-gnueabi-

[zhaojq@virtual-machine]# tar -zxvf curl-7.49.0.tar.gz
[zhaojq@virtual-machine]# cd curl-7.49.0/
[zhaojq@virtual-machine]# ./configure --prefix=/home/zhaojq/libcurl --host=arm-none-linux CC=arm-none-linux-gnueabi-gcc CXX=arm-none-linux-gnueabi-g++
[zhaojq@virtual-machine]# make
[zhaojq@virtual-machine]# make install
生成成功

交叉编译后的文件在/home/zhaojq/libcurl目录下
[zhaojq@virtual-machine libcurl]# ls
bin  include  lib  share

libcurl头文件在include/curl目录
[zhaojq@virtual-machine libcurl/include/curl]# ls
curlbuild.h  curl.h  curlrules.h  curlver.h  easy.h  mprintf.h  multi.h  stdcheaders.h  typecheck-gcc.h

交叉编译后的动态库文件在lib目录
[zhaojq@virtual-machine libcurl/lib]# ls
libcurl.a  libcurl.la  libcurl.so  libcurl.so.4  libcurl.so.4.4.0  pkgconfig

编译test实例
https://curl.haxx.se/libcurl/c/example.html
post-callback An example source code that issues a HTTP POST and we provide the actual data through a read callback.

[zhaojq@virtual-machine]# vim libcurl_test.cpp

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <curl/curl.h>
  4.    
  5. const char data[]="this is what we post to the silly web server";
  6.    
  7. struct WriteThis {
  8.   const char *readptr;
  9.   long sizeleft;
  10. };
  11.    
  12. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
  13. {
  14.   struct WriteThis *pooh = (struct WriteThis *)userp;
  15.    
  16.   if(size*nmemb < 1)
  17.     return 0;
  18.    
  19.   if(pooh->sizeleft) {
  20.     *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
  21.     pooh->readptr++; /* advance pointer */
  22.     pooh->sizeleft--; /* less data left */
  23.     return 1; /* we return 1 byte at a */
  24.   }
  25.    
  26.   return 0; /* no more data left to deliver */
  27. }
  28.    
  29. int main(void)
  30. {
  31.   CURL *curl;
  32.   CURLcode res;
  33.    
  34.   struct WriteThis pooh;
  35.    
  36.   pooh.readptr = data;
  37.   pooh.sizeleft = (long)strlen(data);
  38.    
  39.   /* In windows, this will init the winsock stuff */
  40.   res = curl_global_init(CURL_GLOBAL_DEFAULT);
  41.   /* Check for errors */
  42.   if(res != CURLE_OK) {
  43.     fprintf(stderr, "curl_global_init() failed: %s\n",
  44.             curl_easy_strerror(res));
  45.     return 1;
  46.   }
  47.    
  48.   /* get a curl handle */
  49.   curl = curl_easy_init();
  50.   if(curl) {
  51.     /* First set the URL that is about to receive our POST. */
  52.     curl_easy_setopt(curl, CURLOPT_URL, "");
  53.    
  54.     /* Now specify we want to POST data */
  55.     curl_easy_setopt(curl, CURLOPT_POST, 1L);
  56.    
  57.     /* we want to use our own read function */
  58.     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  59.    
  60.     /* pointer to pass to our read function */
  61.     curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
  62.    
  63.     /* get verbose debug output please */
  64.     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  65.    
  66.     /*
  67.       If you use POST to a HTTP 1.1 server, you can send data without knowing
  68.       the size before starting the POST if you use chunked encoding. You
  69.       enable this by adding a header like "Transfer-Encoding: chunked" with
  70.       CURLOPT_HTTPHEADER. With HTTP 1.0 or without chunked transfer, you must
  71.       specify the size in the request.
  72.     */
  73. #ifdef USE_CHUNKED
  74.     {
  75.       struct curl_slist *chunk = NULL;
  76.    
  77.       chunk = curl_slist_append(chunk, "Transfer-Encoding: chunked");
  78.       res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
  79.       /* use curl_slist_free_all() after the *perform() call to free this
  80.          list again */
  81.     }
  82. #else
  83.     /* Set the expected POST size. If you want to POST large amounts of data,
  84.        consider CURLOPT_POSTFIELDSIZE_LARGE */
  85.     curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, pooh.sizeleft);
  86. #endif
  87.    
  88. #ifdef DISABLE_EXPECT
  89.     /*
  90.       Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue"
  91.       header. You can disable this header with CURLOPT_HTTPHEADER as usual.
  92.       NOTE: if you want chunked transfer too, you need to combine these two
  93.       since you can only set one list of headers with CURLOPT_HTTPHEADER. */
  94.    
  95.     /* A less good option would be to enforce HTTP 1.0, but that might also
  96.        have other implications. */
  97.     {
  98.       struct curl_slist *chunk = NULL;
  99.    
  100.       chunk = curl_slist_append(chunk, "Expect:");
  101.       res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
  102.       /* use curl_slist_free_all() after the *perform() call to free this
  103.          list again */
  104.     }
  105. #endif
  106.    
  107.     /* Perform the request, res will get the return code */
  108.     res = curl_easy_perform(curl);
  109.     /* Check for errors */
  110.     if(res != CURLE_OK)
  111.       fprintf(stderr, "curl_easy_perform() failed: %s\n",
  112.               curl_easy_strerror(res));
  113.    
  114.     /* always cleanup */
  115.     curl_easy_cleanup(curl);
  116.   }
  117.   curl_global_cleanup();
  118.   return 0;
  119. }

[zhaojq@virtual-machine]# arm-none-linux-gnueabi-g++ -lcurl -I/home/kt/libcurl/include -L/home/kt/libcurl/lib -o libcurl_test libcurl.cpp
编译通过,在当前目录生成libcurl_test可执行文件

/home/zhaojq/libcurl目录下的所有文件和pkgconfig目录都拷贝到ARM设备上文件系统的/lib目录,
libcurl_test拷贝到ARM设备上

执行./libcurl_test
./libcurl_test: error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory

libcurl运行需要libssl.so.1.0.0的库,ARM移植openssl-1.0.0s.tar.gz详见另一篇文章
http://blog.csdn.net/miaodichiyou/article/details/50385049



移植成功再次执行./libcurl_test
[root@ARM /]# ./libcurl_test 
*   Trying 93.184.216.34...
* Connected to example.com (93.184.216.34) port 80 (#0)
> POST /index.cgi HTTP/1.1
Host: example.com
Accept: */*
Content-Length: 44
Content-Type: application/x-www-form-urlencoded


* We are completely uploaded and fine
< HTTP/1.1 404 Not Found
< Cache-Control: max-age=604800
< Content-Type: text/html
< Date: Thu, 19 May 2016 02:45:50 GMT
< Expires: Thu, 26 May 2016 02:45:50 GMT
< Server: EOS (lax004/2813)
< Content-Length: 460


         "">

       
                404 - Not Found
       
       
               

404 - Not Found


               
       

* Connection #0 to host example.com left intact
移植成功。

阅读(3422) | 评论(0) | 转发(0) |
0

上一篇:libcurl教程

下一篇:Http报文格式解析

给主人留下些什么吧!~~