设计流程

基于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系统应用软件编写和编译

主要初始化代码:

 #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "curl/curl.h" #define false 0
#define true 1
//#define POSTURL "http://www.xiami.com/member/login"
//#define POSTURL "http://172.16.1.178:8000/api/pushOBEData"
#define POSTURL "https://172.16.1.178:444/api/pushOBEData"
#define POSTFIELDS "email=myemail@163.com&password=mypassword&autologin=1&submit=登 录&type="
size_t process_data(void *buffer, size_t size, size_t nmemb, void *user_p)
{
char bufR[];
char msg[];
size_t return_size = ; return_size = fwrite(buffer,size, nmemb, user_p);
fflush(user_p);
printf("%s>%d-%s\n", __func__, return_size, buffer);
return (size*nmemb);
} int main(int argc, char **argv)
{
// 初始化libcurl
CURLcode return_code;
char *pCaPath = NULL; return_code = curl_global_init(CURL_GLOBAL_ALL);
if (CURLE_OK != return_code)
{
printf("init libcurl failed.\n");
return -;
} // 获取easy handle
CURL *easy_handle = curl_easy_init();
if (NULL == easy_handle)
{
printf("get a easy handle failed.\n");
return -;
} struct curl_slist* headers = NULL; //定义Curl的头部结构体 //---------------------给curl要发送的报文添加头部----------------------
headers = curl_slist_append(headers, "Accept:application/json"); //表示本地cURL接受报文为json
headers = curl_slist_append(headers, "Content-Type:application/json"); //请求我们发送的报文为json,注意这里一定要说明自己发送的信息为JSON类型的,否则对方使用的应用层函数可能无法正确的识别解析
// headers = curl_slist_append(headers, "charset:utf-8"); //表示我们发送的报文的编码格式为utf-8类型的格式
//--------------------------------------------------------------------- curl_easy_setopt(easy_handle, CURLOPT_HTTPHEADER, headers); //给cURL结构体添加我们刚刚组成好的头部
FILE *fp ; if(!(fp = fopen("data.html", "ab+")))
{
printf("fopen erro\n");
return -;
}
fseek(fp, , SEEK_SET);
// 设置easy handle属性
curl_easy_setopt(easy_handle, CURLOPT_URL, POSTURL);
//curl_easy_setopt(easy_handle,CURLOPT_POSTFIELDS,POSTFIELDS); //post参数
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, &process_data);
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(easy_handle,CURLOPT_POST,); //设置问非0表示本次操作为post
//curl_easy_setopt(easy_handle,CURLOPT_VERBOSE,1); //打印调试信息
// curl_easy_setopt(easy_handle, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
// curl_easy_setopt(easy_handle,CURLOPT_HEADER,1); //将响应头信息和相应体一起传给write_data
// curl_easy_setopt(easy_handle,CURLOPT_FOLLOWLOCATION,1); //设置为非0,响应头信息location
// curl_easy_setopt(easy_handle,CURLOPT_COOKIEFILE,"curlposttest.txt"); if(NULL == pCaPath)
{
curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYPEER, );//设定为不验证证书和HOST
curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYHOST, );
}
else
{
curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYPEER, );
curl_easy_setopt(easy_handle, CURLOPT_CAINFO, pCaPath);
} // 执行数据请求
printf("hello https---------------\n");
while()
{
char testbuf[];
static testCnn = ; testCnn++;
snprintf(testbuf,sizeof(testbuf), "test cnn = %d", testCnn);
curl_easy_setopt(easy_handle,CURLOPT_POSTFIELDS,testbuf); //post参数
return_code = curl_easy_perform(easy_handle);
if (return_code != CURLE_OK)
{
switch(return_code)
{
case CURLE_UNSUPPORTED_PROTOCOL:
fprintf(stderr,"不支持的协议,由URL的头部指定\n");
case CURLE_COULDNT_CONNECT:
fprintf(stderr,"不能连接到remote主机或者代理\n");
case CURLE_HTTP_RETURNED_ERROR:
fprintf(stderr,"http返回错误\n");
case CURLE_READ_ERROR:
fprintf(stderr,"读本地文件错误\n");
default:
fprintf(stderr,"返回值:%d\n",return_code);
}
return -;
}
sleep();
}
// 释放资源
sleep();
fclose(fp);
curl_easy_cleanup(easy_handle);
curl_global_cleanup(); return ;
}

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

curl支持HTTP和https的更多相关文章

  1. curl 支持 http2

    让 curl 支持 HTTP2 我们需要安装 nghttp2(http2 的 C 语言库) 源码安装 安装 nghttp2 git clone https://github.com/tatsuhiro ...

  2. CentOS 6.8 curl支持的NSS修改为OpenSSL的方法

    在CentOS 6.8的系统里面的curl支持的https是nss版本的,而不是openssl,所以在php使用curl访问https的时候会报Unable to load client key -8 ...

  3. [archlinux][tls] archlinux下使curl支持sslv3的方法

    描述 在archlinux下,希望使用curl产生一条使用sslv3的https访问请求. 使用curl的如下命令: curl -k -vvv --sslv3 https://192.168.7.9: ...

  4. php之curl实现http与https请求的方法

    原文地址:http://m.jb51.net/show/56492   这篇文章主要介绍了php之curl实现http与https请求的方法,分别讲述了PHP访问http网页与访问https网页的实例 ...

  5. Https系列之三:让服务器同时支持http、https,基于spring boot

    Https系列会在下面几篇文章中分别作介绍: 一:https的简单介绍及SSL证书的生成二:https的SSL证书在服务器端的部署,基于tomcat,spring boot三:让服务器同时支持http ...

  6. 牛客网Java刷题知识点之UDP协议是否支持HTTP和HTTPS协议?为什么?TCP协议支持吗?

    不多说,直接上干货! 福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号:   大数据躺过的坑      Java从入门到架构师      人工智能躺过的坑          ...

  7. Spring Boot项目如何同时支持HTTP和HTTPS协议

    如今,企业级应用程序的常见场景是同时支持HTTP和HTTPS两种协议,这篇文章考虑如何让Spring Boot应用程序同时支持HTTP和HTTPS两种协议. 准备 为了使用HTTPS连接器,需要生成一 ...

  8. nginx将http升级到https并且同时支持http和https两种请求、http自动转向https

    1.http升级到https 1.1.检查 Nginx 是否支持 SSL /usr/local/nginx/sbin/nginx -V configure arguments中是否有--with-ht ...

  9. 【Nginx】将http升级到https并且同时支持http和https两种请求

    一.如何将http升级到https 需要满足下面三个: 1.域名 2.nginx 3.SSL证书     一般第三方证书颁发机构下发的证书是收费的,一年好几千.    1) 从腾讯云申请免费的SSL证 ...

随机推荐

  1. Python struct模块

    有的时候需要用python处理二进制数据,比如,存取文件,socket操作时.这时候,可以使用python的struct模块来完成.可以用 struct来处理c语言中的结构体. struct模块中最重 ...

  2. 测试驱动开发实践3————从testList开始

    [内容指引] 运行单元测试: 装配一条数据: 模拟更多数据测试列表: 测试无搜索列表: 测试标准查询: 测试高级查询. 一.运行单元测试 我们以文档分类(Category)这个领域类为例,示范如何通过 ...

  3. 新概念英语(1-103)The French Test

    Lesson 103 The French test 法语考试 Listen to the tape then answer this question. How long did the exam ...

  4. SpringBoot应用的集成测试

    一.概念和定义 进行软件开发的时候,我们会写很多代码,不过,再过六个月(甚至一年以上)你知道自己的代码怎么运作么?通过测试(单元测试.集成测试.接口测试)可以保证系统的可维护性,当我们修改了某些代码时 ...

  5. 新概念英语(1-43)Hurry up!

    新概念英语(1-43)Hurry up! How do you know Sam doesn't make the tea very often? A:Can you make the tea, Sa ...

  6. "共振式”项目管理

    "共振式”项目管理--是我第一个提出的吗?:) 脑子里突然想到项目管理的一些事情,然后想到项目其实是有节奏的,项目中的人员其实如果找到了这个节奏,踏准了节奏,一切将是顺风顺水. 刚准备动笔时 ...

  7. JS中的数据类型和转换

    一.JS中的数据类型 js中的数据类型可以分为五种:number .string .boolean. underfine .null. number:数字类型 ,整型浮点型都包括. string:字符 ...

  8. ansible批量加用户

    ansible批量加用户 1.生成密码 pip install passlib python -c "from passlib.hash import sha512_crypt; print ...

  9. tkinter打招呼

    import tkinter as tk #导入tkinter模块声明为tk class App:#创建一个类名称为App def __init__(self,master):#传入的参数顶层窗口在这 ...

  10. Delphi X10.2 + FireDAC 使用 SQL 语句 UPDATE

    MainForm.Conn.StartTransaction; UserManagerQuery.SQL.Clear; UserManagerQuery.SQL.Text := 'UPDATE tab ...