从Arduino发送正确的帖子请求

时间:2018-06-04 04:47:12

标签: http post arduino request

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

也已经从NodeMCU-ESP8266上做过了,但我不知道为什么这个巨头变得如此棘手

从这段代码开始,一切顺利,除了我从未收到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成功连接到互联网以及“客户端连接”(服务器,80)&#39;由于它打印了“发送邮件请求”,但我不知道为什么请求捕获者永远不会收到任何帖子请求,我测试了“请求捕获者”#39;使用在线应用程序以及NodeMCU,除了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(); 

请帮忙,任何提示都会非常有用

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我检查了协议的标题,所以这里是最终的工作方式

if (client.connect(server, 80)) { 
    Serial.print(" Sending Post request ");
    client.println("POST /test HTTP/1.0"); 
    client.println("Host: abc.requestcatcher.com");     
    client.println("Connection: close");
    client.println("Content-Length: 0");          //-------   I missed  0
    client.println("Content-Type: application/x-www-form-urlencoded"); 
    client.println("");                                    //-------  I missed  ""

    Serial.println("Server response");
    char c = client.read();
    Serial.println(c);
    client.stop();
  }

太糟糕,形成服务器响应我得到“⸮”当requestcatcher实际发送“请求被捕获”我不太确定http请求后的行,你能不能给它一点推动?。

而且这也非常重要我在顶部犯了一个错误,我不知道这会被视为一个错字但服务器必须以这种方式编写,否则服务器永远不会得到POST请求

char server[] = "abc.requestcatcher.com"; 

避免像这样设置服务器

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