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
-
#include <stdio.h>
-
#include <string.h>
-
#include <curl/curl.h>
-
-
const char data[]="this is what we post to the silly web server";
-
-
struct WriteThis {
-
const char *readptr;
-
long sizeleft;
-
};
-
-
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
-
{
-
struct WriteThis *pooh = (struct WriteThis *)userp;
-
-
if(size*nmemb < 1)
-
return 0;
-
-
if(pooh->sizeleft) {
-
*(char *)ptr = pooh->readptr[0]; /* copy one single byte */
-
pooh->readptr++; /* advance pointer */
-
pooh->sizeleft--; /* less data left */
-
return 1; /* we return 1 byte at a */
-
}
-
-
return 0; /* no more data left to deliver */
-
}
-
-
int main(void)
-
{
-
CURL *curl;
-
CURLcode res;
-
-
struct WriteThis pooh;
-
-
pooh.readptr = data;
-
pooh.sizeleft = (long)strlen(data);
-
-
/* In windows, this will init the winsock stuff */
-
res = curl_global_init(CURL_GLOBAL_DEFAULT);
-
/* Check for errors */
-
if(res != CURLE_OK) {
-
fprintf(stderr, "curl_global_init() failed: %s\n",
-
curl_easy_strerror(res));
-
return 1;
-
}
-
-
/* get a curl handle */
-
curl = curl_easy_init();
-
if(curl) {
-
/* First set the URL that is about to receive our POST. */
-
curl_easy_setopt(curl, CURLOPT_URL, "");
-
-
/* Now specify we want to POST data */
-
curl_easy_setopt(curl, CURLOPT_POST, 1L);
-
-
/* we want to use our own read function */
-
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
-
-
/* pointer to pass to our read function */
-
curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
-
-
/* get verbose debug output please */
-
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
-
-
/*
-
If you use POST to a HTTP 1.1 server, you can send data without knowing
-
the size before starting the POST if you use chunked encoding. You
-
enable this by adding a header like "Transfer-Encoding: chunked" with
-
CURLOPT_HTTPHEADER. With HTTP 1.0 or without chunked transfer, you must
-
specify the size in the request.
-
*/
-
#ifdef USE_CHUNKED
-
{
-
struct curl_slist *chunk = NULL;
-
-
chunk = curl_slist_append(chunk, "Transfer-Encoding: chunked");
-
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
-
/* use curl_slist_free_all() after the *perform() call to free this
-
list again */
-
}
-
#else
-
/* Set the expected POST size. If you want to POST large amounts of data,
-
consider CURLOPT_POSTFIELDSIZE_LARGE */
-
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, pooh.sizeleft);
-
#endif
-
-
#ifdef DISABLE_EXPECT
-
/*
-
Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue"
-
header. You can disable this header with CURLOPT_HTTPHEADER as usual.
-
NOTE: if you want chunked transfer too, you need to combine these two
-
since you can only set one list of headers with CURLOPT_HTTPHEADER. */
-
-
/* A less good option would be to enforce HTTP 1.0, but that might also
-
have other implications. */
-
{
-
struct curl_slist *chunk = NULL;
-
-
chunk = curl_slist_append(chunk, "Expect:");
-
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
-
/* use curl_slist_free_all() after the *perform() call to free this
-
list again */
-
}
-
#endif
-
-
/* 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);
-
}
-
curl_global_cleanup();
-
return 0;
-
}
[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
移植成功。
阅读(3437) | 评论(0) | 转发(0) |