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系统应用软件编写和编译
主要初始化代码:
#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的更多相关文章
- curl 支持 http2
让 curl 支持 HTTP2 我们需要安装 nghttp2(http2 的 C 语言库) 源码安装 安装 nghttp2 git clone https://github.com/tatsuhiro ...
- CentOS 6.8 curl支持的NSS修改为OpenSSL的方法
在CentOS 6.8的系统里面的curl支持的https是nss版本的,而不是openssl,所以在php使用curl访问https的时候会报Unable to load client key -8 ...
- [archlinux][tls] archlinux下使curl支持sslv3的方法
描述 在archlinux下,希望使用curl产生一条使用sslv3的https访问请求. 使用curl的如下命令: curl -k -vvv --sslv3 https://192.168.7.9: ...
- php之curl实现http与https请求的方法
原文地址:http://m.jb51.net/show/56492 这篇文章主要介绍了php之curl实现http与https请求的方法,分别讲述了PHP访问http网页与访问https网页的实例 ...
- Https系列之三:让服务器同时支持http、https,基于spring boot
Https系列会在下面几篇文章中分别作介绍: 一:https的简单介绍及SSL证书的生成二:https的SSL证书在服务器端的部署,基于tomcat,spring boot三:让服务器同时支持http ...
- 牛客网Java刷题知识点之UDP协议是否支持HTTP和HTTPS协议?为什么?TCP协议支持吗?
不多说,直接上干货! 福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 ...
- Spring Boot项目如何同时支持HTTP和HTTPS协议
如今,企业级应用程序的常见场景是同时支持HTTP和HTTPS两种协议,这篇文章考虑如何让Spring Boot应用程序同时支持HTTP和HTTPS两种协议. 准备 为了使用HTTPS连接器,需要生成一 ...
- nginx将http升级到https并且同时支持http和https两种请求、http自动转向https
1.http升级到https 1.1.检查 Nginx 是否支持 SSL /usr/local/nginx/sbin/nginx -V configure arguments中是否有--with-ht ...
- 【Nginx】将http升级到https并且同时支持http和https两种请求
一.如何将http升级到https 需要满足下面三个: 1.域名 2.nginx 3.SSL证书 一般第三方证书颁发机构下发的证书是收费的,一年好几千. 1) 从腾讯云申请免费的SSL证 ...
随机推荐
- apache的重写规则
RewriteEngine OnRewriteCond %{REQUEST_FILENAME} .*\.(jpg|jpeg|gif|png) [NC]RewriteRule .* http://i8. ...
- Python内置函数(40)——dir
英文文档: dir([object]) Without arguments, return the list of names in the current local scope. With an ...
- kubernetes入门(08)kubernetes单机版的安装和使用
kubectl get - 类似于 docker ps ,查询资源列表 kubectl describe - 类似于 docker inspect ,获取资源的详细信息 kubectl logs - ...
- Spring Security 入门(1-5)Spring Security - 匿名认证
匿名认证 对于匿名访问的用户,Spring Security 支持为其建立一个匿名的 AnonymousAuthenticationToken 存放在 SecurityContextHolder 中, ...
- Window7系统下安装jdk
根据电脑的操作系统下载相对于的jdk版本(32位或64位),我安装的是:java_jdk1.7 [计算机]——[属性]——[高级系统设置]——高级——[环境变量] 系统变量——>新建JAVA_H ...
- spring boot定制Jackson ObjectMapper,为什么不生效
先说结论: 项目中定制了spring 的redisTemplate,而这个template没有使用我自定义的Jackson ObjectMapper.所以不生效. 下面是详细过程: 起因是spring ...
- 上传视频使用ffmpeg自动截取缩略图
上传视频之后,有的需要显示缩略图,而不是仅仅显示视频名称的列表,这时候就需要对上传的视频截取缩略图. 简单粗暴点,将以下代码作为工具类复制粘贴即可: package com.util; import ...
- Python基础题
1. 执行Python脚本的两种方式: Chmod +x 脚本 ./脚本(路径的方式) Python 脚本 2. 简述位.字节的关系 一个字节=8位 3. 简述ASCII.unicode.utf-8/ ...
- tornada模板学习笔记
import tornado.web import tornado.httpserver import tornado.ioloop import tornado.options import os. ...
- 一种dubbo逻辑路由方案(服务化隔离环境)
背景介绍 现在很多的公司都在用dubbo.springcloud做为服务化/微服务的开发框架,服务化之后应用越来越多,链路越来越长,服务环境的治理变的很困难.比如:研发团队的人很多的,同时有几个分支在 ...