我正在使用ESPAsyncWebServer库与使用ESP32创建的WiFi AP上的客户端进行交互。我正在尝试部署一个强制验证用户名和密码的门户网站。
#include <WiFi.h>
#include <DNSServer.h>
#include "ESPAsyncWebServer.h"
DNSServer dnsServer;
AsyncWebServer server(80);
class CaptiveRequestHandler : public AsyncWebHandler {
public:
bool canHandle(AsyncWebServerRequest *request){
return true;
}
CaptiveRequestHandler() {}
virtual ~CaptiveRequestHandler() {}
void handleRequest(AsyncWebServerRequest *request)
{
AsyncResponseStream *response = request->beginResponseStream("text/html");
if(request->url() == "/newuser")
{
int params = request->params();
for(int i=0;i<params;i++)
{
AsyncWebParameter* p = request->getParam(i);
if(p->isFile())
{
Serial.printf("FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size());
}
else if(p->isPost())
{
Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
else
{
Serial.printf("GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
}
response->print("<!DOCTYPE html><html><head><title>Captive Portal</title></head><body>");
response->print("<p>Hello!</p>");
response->printf("<p>You were trying to reach: http://%s%s</p>", request->host().c_str(), request->url().c_str());
response->printf("<p>Try opening <a href='http://%s'>this link</a> instead</p>", WiFi.softAPIP().toString().c_str());
response->print("</body></html>");
}
request->send(response);
}
};
void setup(){
//other setup stuff...
WiFi.mode(WIFI_AP);
WiFi.softAP("Project WiFi");
Serial.begin(115200);
dnsServer.start(53, "*", WiFi.softAPIP());
server.addHandler(new CaptiveRequestHandler()).setFilter(ON_AP_FILTER);//only when requested from AP
//more handlers...
server.begin();
}
void loop(){
dnsServer.processNextRequest();
delay(1000);
}
问题是由于某种原因,我无法获取POST参数。
我尝试使用POST Man发送POST请求,但是它根本不会在串行监视器中列出参数。它与GET Request一起工作。