Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js HTTPClient获取节点JS服务器时出错(行为怪异)_Node.js_Arduino_Esp8266_Arduino Esp8266_Esp32 - Fatal编程技术网

Node.js HTTPClient获取节点JS服务器时出错(行为怪异)

Node.js HTTPClient获取节点JS服务器时出错(行为怪异),node.js,arduino,esp8266,arduino-esp8266,esp32,Node.js,Arduino,Esp8266,Arduino Esp8266,Esp32,问题:在运行ESP32脚本的第一分钟,post请求产生HTTPC\u错误\u连接被拒绝,因此没有数据到达服务器。在第一分钟之后,一些请求丢失,但大部分请求每隔2秒到达服务器(这是应该的) 向服务器发送数据的函数: void sendPostData(String data) { // Send the post data to the server http.begin(SERVER_IP); // Begin the HTTP connection http.addHeader(

问题:在运行ESP32脚本的第一分钟,
post
请求产生
HTTPC\u错误\u连接被拒绝
,因此没有数据到达服务器。在第一分钟之后,一些请求丢失,但大部分请求每隔2秒到达服务器(这是应该的)

向服务器发送数据的函数:

void sendPostData(String data) {
  // Send the post data to the server
  http.begin(SERVER_IP);  // Begin the HTTP connection
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  int httpResponseCode = http.POST("val=" + data);
  http.writeToStream(&Serial);
  http.end();
}
节点JS服务器:

var express = require("express");
var bodyParser = require("body-parser");
var app = express();

app.use(bodyParser.urlencoded({ extended: false }));

app.post('/', function (req, res) {
    console.log(req.body);
    res.end();
});

app.listen(80);
如果我使用一个测试网站来接收不是我的服务器的POST请求,例如,
requestcatcher.com
,则不会丢失任何请求。反之亦然,如果我使用网站发送POST请求,例如
hurl.eu
,那么我的服务器没有任何问题

这是ESP32发出的post请求:

POST / HTTP/1.0
Host: sadasdasd.requestcatcher.com
Connection: close
Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0
Connection: close
Content-Length: 10
Content-Type: application/x-www-form-urlencoded
User-Agent: ESP32HTTPClient

尝试使用JSON发送数据,可以使用
ArduinoJson

然后在
loop()
方法中使用这样的代码

  StaticJsonBuffer<300> JSONbuffer;   //Declaring static JSON buffer
  JsonObject& JSONencoder = JSONbuffer.createObject();

  JSONencoder["val"] = data;
  char JSONmessageBuffer[300];
  JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));


  HTTPClient http;    //Declare object of class HTTPClient

  http.begin(SERVER_IP);      //Specify request destination
  http.addHeader("Content-Type", "application/json");  //Specify content-type header

  int httpCode = http.POST(JSONmessageBuffer);   //Send the request
  String payload = http.getString();

  http.end(); 
  delay(1000);
StaticJsonBuffer-JSONbuffer//声明静态JSON缓冲区
JsonObject&JSONencoder=JSONbuffer.createObject();
JSONECODER[“val”]=数据;
char-JSONmessageBuffer[300];
prettyPrintTo(JSONmessageBuffer,sizeof(JSONmessageBuffer));
HTTPClient-http//声明HTTPClient类的对象
http.begin(服务器IP)//指定请求目的地
addHeader(“内容类型”、“应用程序/json”)//指定内容类型标题
int-httpCode=http.POST(JSONmessageBuffer)//发送请求
字符串有效负载=http.getString();
http.end();
延迟(1000);

还是同样的问题,前两个~请求发送大约需要1分钟,然后就可以了。