How to make a correct POST with http client request on ESP32 (error: "DNS Failed for sitename")
How to make a correct POST with http client request on ESP32 (error: "DNS Failed for sitename")
我正在尝试向我设置的网站发送一个 POST 请求,使用 ESP32,其中包含 2 条数据。我曾尝试使用站点 "reqbin.com" 发送包含相同数据的 POST 请求,因此我认为数据本身不是问题。
下面是我的代码:
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 和站点 returns。但是,在 ESP32 上,它 returns 错误:
[E][WiFiGeneric.cpp:654] hostByName(): DNS Failed for sitename.com/data.php
我不确定我请求的语法是否不正确。
更新:更改了我的循环并将服务器地址更改为:
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);
现在出现错误:
socket error on fd 57, errno: 104, "Connection reset by peer"
为 Codebreaker 编辑:
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 编码数据而不是纯文本。所以试试
在 setup()
之前定义以下内容
int port = 80;
String response;
int statusCode = 0;
String serverAddress = "sitename.com";
HttpClient http = HttpClient(wifi, serverAddress, port);
并且在你的循环中你构建 post 如下
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);
如果可行,请将字符串 class 替换为这样的字符数组
char serverAddress[] = "www.test.com"; // server address
我正在尝试向我设置的网站发送一个 POST 请求,使用 ESP32,其中包含 2 条数据。我曾尝试使用站点 "reqbin.com" 发送包含相同数据的 POST 请求,因此我认为数据本身不是问题。
下面是我的代码:
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 和站点 returns。但是,在 ESP32 上,它 returns 错误:
[E][WiFiGeneric.cpp:654] hostByName(): DNS Failed for sitename.com/data.php
我不确定我请求的语法是否不正确。
更新:更改了我的循环并将服务器地址更改为:
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);
现在出现错误:
socket error on fd 57, errno: 104, "Connection reset by peer"
为 Codebreaker 编辑:
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 编码数据而不是纯文本。所以试试 在 setup()
之前定义以下内容 int port = 80;
String response;
int statusCode = 0;
String serverAddress = "sitename.com";
HttpClient http = HttpClient(wifi, serverAddress, port);
并且在你的循环中你构建 post 如下
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);
如果可行,请将字符串 class 替换为这样的字符数组
char serverAddress[] = "www.test.com"; // server address