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執行文件即可。

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM