WiFiClientSecure ESP8266上的400错误请求

时间:2019-03-17 14:05:17

标签: arduino esp8266 nodemcu arduino-esp8266

  

我尝试用ESP8266测试请求,我想使用该请求   HTTPS,但请求结果始终是400个错误请求,因此,   我写错的代码???

#include <ESP8266WiFi.h>
    #include <WiFiClientSecure.h>

    #ifndef STASSID
    #define STASSID "xxxxxxx"
    #define STAPSK  "xxxxxxxxx"
    #endif

    const char* ssid = "xxxxx";
    const char* password = "xxxxxxxx";

    const char* host = "webhook.site";
    const int httpsPort = 443;



    const char fingerprint[] PROGMEM = "09:F6:00:C3:67:B0:80:14:34:E3:08:D7:4A:64:20:85:83:F4:80:A1";

    void setup() {
      Serial.begin(115200);
      Serial.println();
      Serial.print("connecting to ");
      Serial.println(ssid);
      WiFi.mode(WIFI_STA);
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());

      // Use WiFiClientSecure class to create TLS connection
      WiFiClientSecure client;
      Serial.print("connecting to ");
      Serial.println(host);

      Serial.printf("Using fingerprint '%s'\n", fingerprint);
      client.setFingerprint(fingerprint);

      if (!client.connect(host, httpsPort)) {
        Serial.println("connection failed");
        return;
      }
      delay(500);
       if (client.verify(fingerprint, host)) {
        Serial.println("certificate matches");
      } else {
        Serial.println("certificate doesn't match");
      }
    delay(500);
      String url = "/4cf42a06-5df4-4459-b8f3-5d29f1ed319d";
      Serial.print("requesting URL: ");
      Serial.println(url);


    String request = String("GET ") + url + " HTTP/1.0 \r\n" +
                     "Host: " + host + "\r\n" +
                     "Accept: *" + "/" + "*\r\n" +
                      "User-Agent: BuilderFailureDetectorESP8266\r\n" +
                      "Connection: close\r\n\r\n";
      Serial.println(request);               
      client.print(request);

      Serial.println("request sent");
      int waitcount = 0;
      while (!client.available() && waitcount++ ) {
         delay(10);
    }
      while(client.available()){
        String line = client.readStringUntil('\r');
         Serial.print(line);
    }
      while (client.connected()) {
        String line = client.readStringUntil('\r');
        Serial.println("headers received"+line);
        if (line == "\r") {
          Serial.println("headers received"+line);
          break;
        }
      }

    }

    void loop() {
    }
  

以下是上面程序的输出

connecting to xxxx
.....
WiFi connected
IP address: 
192.168.1.13
connecting to webhook.site
Using fingerprint '09:F6:00:C3:67:B0:80:14:34:E3:08:D7:4A:64:20:85:83:F4:80:A1'
certificate matches
requesting URL: /4cf42a06-5df4-4459-b8f3-5d29f1ed319d
GET /4cf42a06-5df4-4459-b8f3-5d29f1ed319d HTTP/1.0 
Host: webhook.site
Accept: */*
User-Agent: BuilderFailureDetectorESP8266
Connection: close


request sent
HTTP/1.0 400 Bad request
Cache-Control: no-cache
Connection: close
Content-Type: text/html

<html><body><h1>400 Bad request</h1>
Your browser sent an invalid request.
</body></html>
  

我的代码有问题吗?大家可以帮我吗??? ...   我正在从Google的几篇文章中寻找以下示例

1 个答案:

答案 0 :(得分:1)

HTTP可能非常留意空白。在您的行中

String request = String("GET ") + url + " HTTP/1.0 \r\n" +

HTTP/1.0后面有多余的空格。删除空间,您应该可以通过“ 400错误的请求”响应。

String request = String("GET ") + url + " HTTP/1.0\r\n" +