curl支持HTTP和https

设计流程

基于curl工具实现https/http,设计初步流程为:linux平台验证→→交叉移植arm板。

 

linux系统下调试http和https

1.1 Linux安装curl

输入命令:sudo apt-get install libcurl4-openssl-dev

安装头文件目录:/usr/include/curl/

 

1.2 Linux系统应用软件编写和编译

主要初始化代码:

  1 #include <stdio.h>  
  2 #include <stdlib.h>  
  3 #include <unistd.h>  
  4 #include <string.h>   
  5 #include "curl/curl.h"  
  6 
  7 #define false 0
  8 #define true  1
  9 //#define POSTURL    "http://www.xiami.com/member/login" 
 10 //#define POSTURL   "http://172.16.1.178:8000/api/pushOBEData" 
 11 #define POSTURL   "https://172.16.1.178:444/api/pushOBEData" 
 12 #define POSTFIELDS "email=myemail@163.com&password=mypassword&autologin=1&submit=登 录&type=" 
 13 size_t process_data(void *buffer, size_t size, size_t nmemb, void *user_p)
 14 {
 15     char bufR[1024];
 16     char msg[1024];
 17     size_t return_size = 0;
 18 
 19     return_size = fwrite(buffer,size, nmemb, user_p);
 20     fflush(user_p);
 21     printf("%s>%d-%s\n", __func__, return_size, buffer);
 22     return (size*nmemb);
 23 }
 24 
 25 int main(int argc, char **argv)
 26 {
 27     // 初始化libcurl
 28     CURLcode return_code;
 29     char *pCaPath = NULL;
 30     
 31     return_code = curl_global_init(CURL_GLOBAL_ALL);
 32     if (CURLE_OK != return_code)
 33     {
 34         printf("init libcurl failed.\n");
 35         return -1;
 36     }
 37    
 38     // 获取easy handle
 39     CURL *easy_handle = curl_easy_init();
 40     if (NULL == easy_handle)
 41     {
 42         printf("get a easy handle failed.\n");
 43         return -1;
 44     }
 45 
 46     struct curl_slist* headers = NULL;      //定义Curl的头部结构体  
 47           
 48     //---------------------给curl要发送的报文添加头部----------------------  
 49     headers = curl_slist_append(headers, "Accept:application/json");            //表示本地cURL接受报文为json  
 50     headers = curl_slist_append(headers, "Content-Type:application/json");      //请求我们发送的报文为json,注意这里一定要说明自己发送的信息为JSON类型的,否则对方使用的应用层函数可能无法正确的识别解析  
 51 //    headers = curl_slist_append(headers, "charset:utf-8");                      //表示我们发送的报文的编码格式为utf-8类型的格式  
 52     //---------------------------------------------------------------------  
 53           
 54     curl_easy_setopt(easy_handle, CURLOPT_HTTPHEADER, headers);            //给cURL结构体添加我们刚刚组成好的头部     
 55     FILE *fp ;
 56 
 57     if(!(fp = fopen("data.html", "ab+")))
 58     {
 59         printf("fopen erro\n");
 60         return -1;
 61     }
 62     fseek(fp, 0 , SEEK_SET);
 63     // 设置easy handle属性
 64     curl_easy_setopt(easy_handle, CURLOPT_URL, POSTURL);
 65     //curl_easy_setopt(easy_handle,CURLOPT_POSTFIELDS,POSTFIELDS); //post参数  
 66     curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, &process_data);
 67     curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, fp);
 68     curl_easy_setopt(easy_handle,CURLOPT_POST,1); //设置问非0表示本次操作为post  
 69     //curl_easy_setopt(easy_handle,CURLOPT_VERBOSE,1); //打印调试信息 
 70 //    curl_easy_setopt(easy_handle, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
 71 //    curl_easy_setopt(easy_handle,CURLOPT_HEADER,1); //将响应头信息和相应体一起传给write_data  
 72 //    curl_easy_setopt(easy_handle,CURLOPT_FOLLOWLOCATION,1); //设置为非0,响应头信息location  
 73 //    curl_easy_setopt(easy_handle,CURLOPT_COOKIEFILE,"curlposttest.txt");     
 74     
 75     if(NULL == pCaPath)
 76     {
 77         curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYPEER, 0);//设定为不验证证书和HOST 
 78         curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYHOST, 0); 
 79     }
 80     else 
 81     { 
 82         curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYPEER, 1); 
 83         curl_easy_setopt(easy_handle, CURLOPT_CAINFO, pCaPath); 
 84     }
 85     
 86     // 执行数据请求
 87     printf("hello https---------------\n");
 88     while(1)
 89     {
 90         char testbuf[100];
 91         static testCnn = 0;
 92         
 93         testCnn++;
 94         snprintf(testbuf,sizeof(testbuf), "test cnn = %d", testCnn);
 95         curl_easy_setopt(easy_handle,CURLOPT_POSTFIELDS,testbuf); //post参数  
 96         return_code = curl_easy_perform(easy_handle);    
 97         if (return_code != CURLE_OK)  
 98         {  
 99             switch(return_code)  
100             {  
101                 case CURLE_UNSUPPORTED_PROTOCOL:  
102                     fprintf(stderr,"不支持的协议,由URL的头部指定\n");  
103                 case CURLE_COULDNT_CONNECT:  
104                     fprintf(stderr,"不能连接到remote主机或者代理\n");  
105                 case CURLE_HTTP_RETURNED_ERROR:  
106                     fprintf(stderr,"http返回错误\n");  
107                 case CURLE_READ_ERROR:  
108                     fprintf(stderr,"读本地文件错误\n");  
109                 default:  
110                     fprintf(stderr,"返回值:%d\n",return_code);  
111             }  
112             return -1;  
113         } 
114         sleep(10);
115     }
116     // 释放资源
117     sleep(10);
118     fclose(fp);
119     curl_easy_cleanup(easy_handle);
120     curl_global_cleanup();
121 
122 
123     return 0;
124 }
View Code

 

 

gcc -o curlTest curlTest.c -l curl

2 ARM板上curl的移植

因为要支持https需要移植openssl 和 curl

其中要注意只有在curl加入openssl才能支持https。

2.1 Openssl移植

开发环境

  Ubuntu 14.04 
  gcc-linaro-arm-linux-gnueabihf-4.9-2014.07

移植步骤

1.从OpenSSL官网下载最新源码 openssl-1.0.2l.tar.gz。 
2.执行下面命名解压缩:tar zxvf openssl-1.0.2l.tar.gz

3.进入刚解压的目录cd openssl-1.0.2l/,执行下面指令,做相应的配置:

./config no-asm shared --prefix=/usr/local/ssl --cross-compile-prefix=/opt/gcc-linaro-arm-linux-gnueabihf-4.9-2014.07_linux/bin/arm-linux-gnueabihf-

no-asm: 是在交叉编译过程中不使用汇编代码代码加速编译过程,原因是它的汇编代码是对arm格式不支持的。

shared :生成动态连接库。

--prefix :指定make install后生成目录的路径,不修改此项则默认为OPENSSLDIR目录(/usr/local/ssl)。

4.修改Makefile:

CC=/opt/gcc-linaro-arm-linux-gnueabihf-4.9-2014.07_linux/bin/arm-linux-gnueabihf-gcc (此处交叉工具链用绝对路径)

删除 CFLAG= 中的-m64

AR=/opt/gcc-linaro-arm-linux-gnueabihf-4.9-2014.07_linux/bin/arm-linux-gnueabihf-ar $(ARFLAGS) r

RANLIB=/opt/gcc-linaro-arm-linux-gnueabihf-4.9-2014.07_linux/bin/arm-linux-gnueabihf-ranlib

NM=/opt/gcc-linaro-arm-linux-gnueabihf-4.9-2014.07_linux/bin/arm-linux-gnueabihf-nm

SHARED_LDFLAGS=

注:上面各项都是修改后的,没有增加内容,只是在原来基础上做修改,故没有列出修改前的内容。

5.执行下面命令,编译OpenSSL库:

make

6.执行下面命令,将编译好的库文件拷贝到指定目录:

make install

7.include下文件在编译程序的时候需要指定include的路径。而lib下在程序运行时会用到,需要将lib下文件拷贝到开发板中。

 

2.2 curl在ARM中交叉移植

开发环境

  Ubuntu 14.04 
  gcc-linaro-arm-linux-gnueabihf-4.9-2014.07

         curl-7.59.0.tar.gz

移植步骤

1.下载最新源码 curl-7.59.0.tar.gz。 
2.执行下面命名解压缩:tar zxvf curl-7.59.0.tar.gz

3.进入刚解压的目录cd curl-7.59.0/,执行下面指令,做相应的配置:

./configure --prefix=/home/huali/usrApp/libcurl/ --with-ssl=/usr/local/ssl --host=arm-linux CC=/opt/gcc-linaro-arm-linux-gnueabihf-4.9-2014.07_linux/bin/arm-linux-gnueabihf-gcc CXX=/opt/gcc-linaro-arm-linux-gnueabihf-4.9-2014.07_linux/bin/arm-linux-gnueabihf-g++

--with-ssl:检测ssl 库所在位置

4.执行下面命令:

 make

5.执行下面命令,将编译好的库文件拷贝到指定目录:

    

 make install

6. 然后我们把libcurl整个文件夹拷贝到板子上

7. 修改环境变量

这里我修改了/etc/profile文件,目的是使修改对所有用户生效,不用每次都修改了。我们在第二行的PATH语句末尾添加“:/lib/bin”就可以了,然后保存退出。输入sync语句同步文件,然后重启即可。

8. 可以看到我们成功安装curl

   

2.3.CMAKE编译

  Cmake编译教程这里不再描述。

  cmake .

  make

  

  在arm板中运行curlTest执行文件即可。

  

 

posted @ 2018-04-24 11:02  Leo_tt  阅读(7959)  评论(0编辑  收藏  举报