ESP8266 使用Post请求跟get请求

近期使用了ESP8266访问一些类似这样网址 https://mp.csdn.net/editor/html该怎么做呢。

首先我们需要知道ESP8266是能运行TCP/IP协议,而诸如MQTT、HTTP都是运行在这个基础之上的。

了解到这一点,我们要做的是了解HTTP这种协议的组成。查询了一些相关资料总结一下,

什么是HTTP?

HTTP是面向事务的应用层协议,建立连接请求的是端口80

在浏览器跟服务器之前的请求和响应的交互,必须按照规定的格式和遵循一定的规则,这些格式跟规则就是超文本传送协议HTTP.

 

HTTP是运行在TCP之上,TCP作为运输层的协议,确保数据的可靠传输

 

 

什么是代理服务器?代理服务器是用来干什么的?

代理服务器是网路实体,又称万维网告诉缓存,把近期的请求跟响应暂存于本地磁盘。

当新的请求到达时,代理服务器发现这个请求跟暂时存放的请求相同,就返回暂存的响应,而不需要按URL的地址再次去互联网访问该资源。

 

 

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要让 ESP8266 接收 POST 请求,你需要执行以下步骤: 1. 通过 WiFi 连接到网络,并获取 IP 地址。 2. 创建一个 TCP 服务器并监听来自客户端的连接。 3. 在服务器上接收来自客户端的请求。 4. 解析请求中的数据。 5. 响应客户端的请求。 下面是一个示例代码,可以帮助你了解如何实现这些步骤: ```c #include <ESP8266WiFi.h> const char* ssid = "YOUR_SSID"; const char* password = "YOUR_PASSWORD"; WiFiServer server(80); void setup() { Serial.begin(115200); delay(10); // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Server started"); } void loop() { // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } // Wait for data from the client while (!client.available()) { delay(1); } // Read the first line of the request String request = client.readStringUntil('\r'); Serial.println(request); // Parse the request to get the method, URL, and HTTP version String method = request.substring(0, request.indexOf(' ')); String url = request.substring(request.indexOf(' ') + 1, request.lastIndexOf(' ')); String httpVersion = request.substring(request.lastIndexOf(' ') + 1); // Check if the method is POST and the URL is correct if (method == "POST" && url == "/your-url") { // Read the headers while (client.available()) { String header = client.readStringUntil('\r'); if (header == "\n") { break; } } // Read the POST data String postBody = ""; while (client.available()) { postBody += client.readStringUntil('\r'); } // Print the POST data Serial.println(postBody); // Send the response client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html>"); client.println("<head>"); client.println("<title>POST Request Received</title>"); client.println("</head>"); client.println("<body>"); client.println("<h1>POST Request Received</h1>"); client.println("<p>" + postBody + "</p>"); client.println("</body>"); client.println("</html>"); delay(100); client.stop(); } } ``` 在上面的代码中,将 `YOUR_SSID` 和 `YOUR_PASSWORD` 替换为你的 WiFi SSID 和密码。在 `loop()` 函数中,我们使用 `server.available()` 检查是否有客户端连接到服务器,并使用 `client.readStringUntil('\r')` 读取请求中的数据。然后,我们解析请求中的数据并检查是否为 POST 请求和正确的 URL。如果是 POST 请求,则读取 POST 数据,并向客户端发送响应。 请注意,此代码只是一个示例,你需要根据自己的需求进行修改。你可以将代码上传到 ESP8266 板子上并运行,然后使用 POST 请求测试它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值