如何使用ESP32上的http客户端请求进行正确的POST(错误:“sitename的DNS失败”)
我正在尝试向我使用ESP32设置的站点发送一个POST请求,其中包含2条数据。我曾尝试使用网站“reqbin.com”发送包含相同数据的POST请求,因此我认为数据本身不是问题所在 下面是我的代码:如何使用ESP32上的http客户端请求进行正确的POST(错误:“sitename的DNS失败”),post,arduino,dns,request,esp32,Post,Arduino,Dns,Request,Esp32,我正在尝试向我使用ESP32设置的站点发送一个POST请求,其中包含2条数据。我曾尝试使用网站“reqbin.com”发送包含相同数据的POST请求,因此我认为数据本身不是问题所在 下面是我的代码: char ssid[] = "{{name}}"; char pass[] = "{{pass}}"; int port = 8080; WiFiClient wifi; void setup() { delay(1000); WiFi.mode(WIFI_OFF); //Prevents
char ssid[] = "{{name}}";
char pass[] = "{{pass}}";
int port = 8080;
WiFiClient wifi;
void setup()
{
delay(1000);
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
WiFi.begin(ssid, pass); //Connect to your WiFi router
Serial.println("");
Serial.print("Connecting");
// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{
String serverAddress = "sitename.com/data.php";
String contentType = "text/plain"
String postData = "t=26.1 h=25.6";
HttpClient http = HttpClient(wifi, serverAddress, port);
http.beginRequest(); //Specify request destination
int httpCode = http.post("/", contentType, postData);
Serial.println("httpCode = " + httpCode);
if (httpCode > 0)
{
String response = http.responseBody();
Serial.println("httpCode === " + httpCode);
Serial.println(response);
}
else
{
Serial.println("Error on POST request");
Serial.println(httpCode);
}
http.endRequest();
}
当使用reqbin时,我得到状态200,站点返回。但是,在ESP32上,它返回错误:
[E][WiFiGeneric.cpp:654] hostByName(): DNS Failed for sitename.com/data.php
socket error on fd 57, errno: 104, "Connection reset by peer"
我不确定我请求的语法是否不正确
更新:更改了我的循环并将服务器地址更改为:
String serverAddress = "sitename.com";
String contentType = "text/plain";
String postData = "t=26.1 h=25.6";
HttpClient http = HttpClient(wifi, serverAddress, port);
http.beginRequest(); //Specify request destination
int httpCode = http.post("/data.php", contentType, postData);
Serial.println("httpCode = " + httpCode);
现在获取错误:
[E][WiFiGeneric.cpp:654] hostByName(): DNS Failed for sitename.com/data.php
socket error on fd 57, errno: 104, "Connection reset by peer"
编辑代码断路器:
String serverAddress = "sitename.com";
HttpClient http = HttpClient(wifi, serverAddress, port);
String response;
int statusCode = 0;
Setup has stayed the same.
void loop()
{
String contentType = "application/x-www-form-urlencoded";
// String postData = temp + " " + humid;
String postData = "?t=26.1&h=25.6";
http.post("/data.php", contentType, postData);
// read the status code and body of the response
statusCode = http.responseStatusCode();
response = http.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
}
我猜服务器需要url编码的数据,而不是纯文本。所以试试看 在安装之前定义以下内容() 在你的循环中,你构建帖子如下
String contentType = "application/x-www-form-urlencoded";
String postData = "?t=26.1&h=25.6";
http.post("/data.php", contentType, postData);
然后阅读回答
// read the status code and body of the response
statusCode = http.responseStatusCode();
response = http.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
如果可以的话,用如下的字符数组替换字符串类
char serverAddress[] = "www.test.com"; // server address
我已经做了您建议的更改,但仍然有错误:[E][WiFiClient.cpp:258]connect():fd 54上的套接字错误,错误号:104,“由对等方重置连接”不确定原因,但不管我做了多少修改,它似乎都会发生。你能发布你的代码的最后版本吗?很难达到移动目标,而且它真的是http的8080端口吗?这通常是websocket,所以端口应该是8080,以便添加到主帖子中以获得更好的格式,1分钟将端口更改为80,注意如何使用?X=值&Y=值来连接post数据