我是靠谱客的博主 贤惠奇迹,这篇文章主要介绍基于STM32封装的HTTP请求,现在分享给大家,希望可以做个参考。

使用STM32封装HTTP协议


通过封装HTTP头,向服务器发送GET请求,通过GET请求上传数据,并获取返回值

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <string.h> #include <stdio.h> #include <stdint.h> char text[30]; char temp[10]; /** * @brief 组HTTP GET报文 * @param pkt 报文缓存指针 * @param key key唯一识别码 * @param data 数据 */ uint32_t HTTP_GETPkt(char *pkt, const char *key, int data) { *pkt = 0; memset(temp, 0, 10); memset(text, 0, 30); // 组装GET请求字符串 sprintf(temp,"%d", data); strcat(text, "auth_key="); strcat(text, key); strcat(text, "&"); strcat(text, "data="); strcat(text, temp); // 向服务器发送GET请求的文件地址 strcat(pkt, "GET /getinfo.php?"); strcat(pkt, text); // 使用1.1版本HTTP strcat(pkt, " HTTP/1.1rn"); // 服务器所在地址,这是自己电脑通过无线局域网建立的服务器 // IP:192.168.1.108,PORT:8888 strcat(pkt, "Host: 192.168.1.108:8888rn"); // 保持连接 strcat(pkt, "Connection: Keep-Alivern"); // 不使用缓存 strcat(pkt, "Cache-Control: no-cachern"); strcat(pkt, "rnrn"); return strlen(pkt); }

通过调用HTTP_GETPkt()函数并传入指定参数,就可以通过局域网上传数据到自己搭建的服务器,当应用于公网时,只需要把IP和PORT做相应修改即可。

STM32 上传源码:https://github.com/PotoYang/STM32_HTTP_DataTrans

最后

以上就是贤惠奇迹最近收集整理的关于基于STM32封装的HTTP请求的全部内容,更多相关基于STM32封装内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部