gpt4 book ai didi

http - 从 Arduino 发送适当的 Post 请求

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:03 26 4
gpt4 key购买 nike

我正在尝试使用以太网屏蔽从我的 Arduino Mega 发送一个 Post 请求,我已经在互联网上尝试了很多代码,但我还没有完成

也已经从 NodeMCU-ESP8266 上完成了,但我不知道为什么 mega 变得如此棘手

从这段代码开始,一切顺利,除了我从未收到 POST 请求,我正在使用这个网站“requestcatcher”来测试 POST 请求

#include <Ethernet.h>
#include <SPI.h>

// Conf. mac
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Server to Post
char server[] = "http://abc.requestcatcher.com/test";

// Starting Ethernet client
EthernetClient client;

// =============== Connecting to internet =============== //

void setup() {
// Open serial communications and wait for port to open:
// wait for serial port to connect. Needed for native USB port only
Serial.begin(9600);
while (!Serial) {
;
}

// Connecting to internet
if (Ethernet.begin (mac) == 0) {
Serial.println("Can’t connect via DHCP");
}

// Give the Ethernet shield a second to initialize
delay(1000);
// Printing the IP Adress
Serial.print ("IP Address: ");
Serial.println(Ethernet.localIP());

}

/////============= Sending Post request ============= ////

void loop() {

Serial.println(" - Post request in process - ");

if (client.connect(server, 80) {
Serial.print(" Sending Post request ");
client.println("POST /test HTTP/1.1");
client.println("Host: abc.requestcatcher.com/");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Content-Length: ");
client.println();
}
else {
Serial.println("Can’t reach the server");
}
// Wait 10 secs
delay(10000);
}

Arduino 像这样通过串行打印

IP Adress: 192.168.100.40
- Post request in process -
Sending Post request
- Post request in process -
Sending Post request
- Post request in process -
Sending Post request

所以我认为这意味着 Arduino 成功连接到互联网并且“client.connect(server, 80)”变为真,因为它打印“发送帖子请求”,但我不知道为什么请求捕获器永远无法获取任何发布请求,我使用在线应用程序以及 NodeMCU 测试了“requestcatcher”,它从除 Arduino 之外的所有应用程序获取发布请求,所以我认为这里一定有问题:

   client.println("POST /test HTTP/1.1"); 
client.println("Host: abc.requestcatcher.com/");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Content-Length: ");
client.println();

请帮忙,任何提示都会很有帮助

最佳答案

您必须删除这部分代码中的 ; 和右括号 if (client.connect(server, 80); { => if (client .connect(server, 80)) {...}

关于http - 从 Arduino 发送适当的 Post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50673535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com