【发布时间】:2018-06-04 04:47:11
【问题描述】:
我正在尝试使用以太网屏蔽从我的 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)' 为真,因为它打印了 'Sending Post request',但我不知道为什么请求捕手永远不会得到任何发布请求,我都使用在线应用程序和 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();
请帮忙,任何提示都会很有帮助
【问题讨论】: