【问题标题】:Sending a proper Post request from Arduino从 Arduino 发送正确的 Post 请求
【发布时间】: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(); 

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

【问题讨论】:

    标签: http post arduino request


    【解决方案1】:

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

    【讨论】:

    • 谢谢,Ivan Sheigets 的回复,我在问题中打错字,我更正了,但我的代码中没有错字,因为它符合要求,我不知道为什么发布请求不是收到
    【解决方案2】:

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

    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/";
    

    【讨论】:

      猜你喜欢
      • 2016-07-03
      • 2015-12-22
      • 2018-05-26
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多