Android: 使用libevent和libcurl去实现http和https服务器,用来测试android客户端程序

news/2024/9/23 1:39:39/

记录一下一篇好的博文:使用libevent和libcurl去实现http和https服务器
http://eleaction01.spaces.eepw.com.cn/articles/article/item/183383

前言

libevent和libcurl都是功能强大的开源库;libevent主要实现服务器,包含了select、epoll等高并发的实现;libcurl实现了curl命令的API封装,主要作为客户端。这两个开源库的安装可以参考我的这篇博客:https://www.cnblogs.com/liudw-0215/p/9917422.html,并且我的代码都提交在了我的github上了,可以点左上角图标,跳转到github,仓库是libcurl。

一、curl的两种使用方法

1、命令行模式
    所谓命令行模式,就是直接linux的命令行直接可以执行的curl命令,curl可以做很多事情,我主要介绍作为客户端发送xml和json数据,因为命令行模式非常要注意格式问题!

(1)发送xml格式数据
  格式如下:
echo '<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv=“http://schemas.xmlsoap.org/soap/envelope/” xmlns:itsm=“http://itsm.soa.csg.cn/”>
<soapenv:Header xmlns:auth=“http://itsm.soa.csg.cn/”>
auth:userlocal_admin</auth:user>
auth:passwordlocal_admin</auth:password>
</soapenv:Header>
soapenv:Body
itsm:accountOper
1


测试虚拟机181106
11.11.22.23
设备帐户
administrator


</itsm:accountOper>
</soapenv:Body>
</soapenv:Envelope> '|curl -X POST -H ‘Content-type:text/xml’ -d @- http://10.94.1.167:80/ITSMWebServer/itsm

说明:
echo后面跟的是xml格式数据,格式一般都是跟第三方平台约定好的,不能发这种格式,接收又是另一种格式,那没法解析了,都要提前约定好的!

中间是“|”管道符,将echo的输出作为curl的输入

POST 说明是post请求

-H 携带的消息头

最后的url,是要发送的地址

(2)发送json格式数据
  格式如下:
curl -H “Content-Type:application/json” -H “appName:spvas” -H “password:123123” -H “pswdHashType:SHA1” -X POST -k -g -d ‘{“param”:[{“objectID”:112,“type”:1,“operate”:1,“operatorID”:100,“result”:0,“time”:1539941168,“policytype”:0}]}’ http://172.16.1.21:9999/rest/spvas/objChange.do
  说明:
  -H 依然是消息头
-d 后面是json格式的数据了

2、libcurl库使用

1、安装
  想要使用libcurl库,首先需要先安装,安装参考我的这篇博客写的很详细:https://www.cnblogs.com/liudw-0215/p/9917422.html

2、使用libcurl的API
  主要就是调用libcurl库的API接口,下面介绍的http的POST请求,libcurl很多接口,不能一一介绍,需要时可以再去查找。

(1)初始化curl句柄

CURL* curl = NULL;
curl = curl_easy_init();
  (2)设置curl的url

curl_easy_setopt(curl, CURLOPT_URL, “http://172.16.1.96:7777/login”);
  (3)开启post请求开关

curl_easy_setopt(curl, CURLOPT_POST, true);
  (4)添加post数据

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_str);
  (5)设定一个处理服务器响应的回调函数

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, deal_response);
  (6)给回调函数传递一个形参

curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseData);
  (7)向服务器发送请求,等待服务器的响应

res = curl_easy_perform(curl);
  3、总体代码
 客户端总体代码如下:

Created by ldw on 2018/11/8.//#include “cJSON.h”#include <curl/curl.h>#include<string.h>#define RESPONSE_DATA_LEN 4096//用来接收服务器一个buffertypedef struct login_response_data
{
login_response_data() {
memset(data, 0, RESPONSE_DATA_LEN);
data_len = 0;
} char data[RESPONSE_DATA_LEN]; int data_len;

}response_data_t;//处理从服务器返回的数据,将数据拷贝到arg中size_t deal_response(void *ptr, size_t n, size_t m, void arg)
{ int count = m
n;

response_data_t *response_data = (response_data_t*)arg;memcpy(response_data->data, ptr, count);response_data->data_len = count;    return response_data->data_len;

}#define POSTDATA "{“username”:“gailun”,“password”:“123123”,“driver”:“yes”}"int main()
{

char *post_str = NULL;CURL* curl = NULL;
CURLcode res;
response_data_t responseData;//专门用来存放从服务器返回的数据    //初始化curl句柄
curl = curl_easy_init();    if(curl == NULL) {        return 1;
}    //封装一个数据协议
/*====给服务端的协议====     http://ip:port/login [json_data]
{username: "gailun",password: "123123",driver:   "yes"
}*** */
//(1)封装一个json字符串
cJSON *root = cJSON_CreateObject();cJSON_AddStringToObject(root, "username", "ldw");
cJSON_AddStringToObject(root, "password", "123123");
cJSON_AddStringToObject(root, "driver", "yes");post_str = cJSON_Print(root);
cJSON_Delete(root);
root = NULL;    //(2) 向web服务器 发送http请求 其中post数据 json字符串    //1 设置curl url
curl_easy_setopt(curl, CURLOPT_URL, "http://172.16.1.96:7777/login");    //客户端忽略CA证书认证 用于https跳过证书认证
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);    //2 开启post请求开关
curl_easy_setopt(curl, CURLOPT_POST, true);    //3 添加post数据    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_str);    //4 设定一个处理服务器响应的回调函数    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, deal_response);    //5 给回调函数传递一个形参
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseData);    //6 向服务器发送请求,等待服务器的响应
res = curl_easy_perform(curl);    if (res != CURLE_OK) {        return 1;
}
curl_easy_cleanup(curl);    
//(3)  处理服务器响应的数据 此刻的responseData就是从服务器获取的数据
/*//成功
{result: "ok",
}
//失败
{result: "error",reason: "why...."
}** */
//(4) 解析服务器返回的json字符串    //cJSON *root;
root = cJSON_Parse(responseData.data);cJSON *result = cJSON_GetObjectItem(root, "result");    if(result && strcmp(result->valuestring, "ok") == 0) {printf("data:%s\n",responseData.data);        //登陆成功return 0;}    else {        //登陆失败cJSON* reason = cJSON_GetObjectItem(root, "reason");        if (reason) {            //已知错误return 1;}        else {            //未知的错误return 1;}        return 1;
}    return 0;

}
复制代码
  这是客户端的总体代码,但是还无法测试,因为没有服务端,下面会介绍用libevent库来搭建http的服务端;因为数据格式是json,所以用到了cJSON,可以到我的github上进行下载,编译命令:g++ login.cpp cJSON.cpp -o login -lcurl

二、libevent库

1、安装
    libevent依然是开源库,使用之前依然需要安装,安装参考我的这篇博客写的很详细:https://www.cnblogs.com/liudw-0215/p/9917422.html

2、搭建http服务器
    安装之后,就可以使用了,主要都是调用libcurl库的API函数,main函数如下:
int main(int argc, char *argv[]) { //自定义信号处理函数 signal(SIGHUP, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler); //默认参数
char httpd_option_listen = “0.0.0.0”; int httpd_option_port = 7777; int httpd_option_daemon = 0; int httpd_option_timeout = 120; //in seconds //获取参数
int c; while ((c = getopt(argc, argv, “l:p:dt:h”)) != -1) { switch © { case ‘l’ :
httpd_option_listen = optarg; break; case ‘p’ :
httpd_option_port = atoi(optarg); break; case ‘d’ :
httpd_option_daemon = 1; break; case ‘t’ :
httpd_option_timeout = atoi(optarg); break; case ‘h’ : default :
show_help();
exit(EXIT_SUCCESS);
}
} //判断是否设置了-d,以daemon运行
if (httpd_option_daemon) {
pid_t pid;
pid = fork(); if (pid < 0) {
perror(“fork failed”);
exit(EXIT_FAILURE);
} if (pid > 0) { //生成子进程成功,退出父进程 exit(EXIT_SUCCESS);
}
} /
使用libevent创建HTTP Server */

//初始化event API    event_init();    //创建一个http server
struct evhttp *httpd;httpd = evhttp_start(httpd_option_listen, httpd_option_port);evhttp_set_timeout(httpd, httpd_option_timeout);    //也可以为特定的URI指定callback
evhttp_set_cb(httpd, "/", httpd_handler, NULL);
evhttp_set_cb(httpd, "/login", login_handler, NULL);    //循环处理events    event_dispatch();evhttp_free(httpd);    return 0;

}
复制代码
   3、测试http服务
  启动服务端

从我的github上下载之后,http服务在libcurl/http_server/这个目录,写Makefile,然后直接make就可以了,如下:

make之后生成了server,执行:./server,启动服务

启动客户端

在libcurl/login/这个目录,执行:g++ login.cpp cJSON.cpp -o login -lcurl,进行编译,生成login,启动客户端:./login,客户端运行结果,如下:

服务端响应结果,如下:

至此,完成了演示,用libcurl和libevent搭建的http服务器与客户端,没有问题。是不是觉得到此就结束了,才没有呢?下面,将要介绍https服务器,那为什么要用https服务器呢?跟随我找到谜底吧!

4、搭建https服务器  
  (1)https介绍
  http传输过程都是明文传输,很不安全;就产生https,进行加密传输,但加密过程并没有那么简单,如下图所示:

说明:

主要经历了两个阶段:

非对称加密过程

通过公钥、私钥和CA证书,进行验证,最终获得会话密钥

对称加密过程

可能会想?直接都用非对称加密得了,为啥用对称加密?因为非对称效率很低,所以要用对称加密!

用非对称过程得到的密钥,对数据进行加密然后传输。

(2)https服务器实现
  libevent库应该从2.1版本之后才支持https的,所以在2.1之前的版本还要单独安装openssl!

mian函数如下:

int main (int argc, char **argv)
{
/*OpenSSL 初始化 */
common_setup ();

if (argc > 1) {        char *end_ptr;        long lp = strtol(argv[1], &end_ptr, 0);        if (*end_ptr) {fprintf(stderr, "Invalid integer\n");            return -1;}        if (lp <= 0) {fprintf(stderr, "Port must be positive\n");            return -1;}        if (lp >= USHRT_MAX) {fprintf(stderr, "Port must fit 16-bit range\n");            return -1;}serverPort = (unsigned short)lp;
}    /* now run http server (never returns) */
return serve_some_http ();

}
  (3)测试https服务器
  启动服务端  
  从我的github上下载之后,http服务在libcurl/https_server/这个目录,写Makefile,然后直接make就可以了;

启动客户端
  修改http的客户端就可以了,如下:
curl_easy_setopt(curl, CURLOPT_URL, “https://172.16.1.96:8080/login”);
//客户端忽略CA证书认证 用于https跳过证书认证curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
  说明:
  在http后面加上“s”;再加上跳过证书认证,就可以了


http://www.ppmy.cn/news/38663.html

相关文章

论文阅读《Point NeRF:Point-based Neural Radiance Fileds》

论文地址&#xff1a;https://arxiv.org/abs/2201.08845 源码地址&#xff1a;https://xharlie.github.io/projects/project_sites/pointnerf 概述 体素神经渲染的方法生成高质量的结果非常耗时&#xff0c;且对不同场景需要重新训练&#xff08;模型不具备泛化能力&#xff09…

springboot(10)异步任务

文章目录1、SpringBoot异步任务1.1使用注解EnableAsync开启异步任务支持1.2使用Async注解标记要进行异步执行的方法1.3controller测试2.异步任务相关限制3.1自定义 Executor3.1.1应用层级&#xff1a;3.1.2方法层级&#xff1a;3.2自定义 Executor (第二种方式)4.1异常处理4.1.…

【python设计模式】3、抽象工厂模式

设计哲学&#xff1a; 抽象工厂模式的哲学思想是面向接口编程&#xff08;Interface Segregation Principle&#xff0c;ISP&#xff09;。这一原则强调&#xff0c;客户端不应该依赖于它不需要的接口&#xff0c;而应该将接口尽可能地细化&#xff0c;只包含客户端所需的方法…

数字藏品系统功能介绍

一、特色功能 1、数字藏品专业的产品打造 为数字藏品提供链上的存证独立的身份认证&#xff0c;所有的产品都是有一连串数字的。 2、数字藏品实现发售&#xff0c;拍卖&#xff0c;申购&#xff0c;盲盒等 数字藏品提供发售渠道&#xff0c;利用多种模式的方式进行发售&…

项目二:电子骰子

项目二&#xff1a;电子骰子 文章目录项目二&#xff1a;电子骰子一、导入(5分钟&#xff09;学习目的二、新授(65分钟)1.预展示结果(5分钟)2.本节课所用的软硬件(5分钟)3.硬件介绍(1分钟)4.图形化块介绍(1分钟)5.单个模块的简单使用(1分钟)6.电子骰子编程逻辑分析(25分钟)7.电…

Python教程:GC垃圾回收

一.GC垃圾回收 1.小整数对象池&#xff1a;Python为了优化速度&#xff0c;使用了小整数对象池&#xff0c; 避免为整数频繁申请和销毁内存空间。Python 对小整数的定义是[-5, 257) 这些整数对象是提前建立好的&#xff0c;不会被垃圾回收 2.大整数对象池&#xff1a;每一个大…

清明-微信小程序

# 云开发接入 初始化云开发 环境保密

探寻人工智能前沿 迎接AIGC时代——CSIG企业行(附一些好玩的创新点)

上周我有幸参加了由中国图像图形学会和合合信息共同举办的CSIG企业行活动。这次活动邀请了多位来自图像描述与视觉问答、图文公式识别、自然语言处理、生成式视觉等领域的学者&#xff0c;他们分享了各自的研究成果和经验&#xff0c;并与现场观众进行了深入的交流和探讨。干货…