如何使用WiFiClientSecure的setCACert()和HTTPClient库?

问题描述 投票:0回答:1

我想设置我的CA根证书(目前可通过WiFiClientSecure库获得)并使用方便的HTTPClient库来提出请求。

怎么做?在the example中,只展示了如何使用WiFiClientSecure手动书写请求。

esp8266 arduino-esp8266
1个回答
0
投票

这是一个迟到的方式,但这篇文章是谷歌返回的一个,所以这是一个很好的方式来帮助像我这样的人刚开始这个。

事实证明,关于WifiClientSecure的所有帖子都已过时(或者根本就没有必要)。

这是使用默认HTTPClient的工作示例。 (不是HttpClient)它需要在begin语句中设置证书的sha1指纹。

void loop() {
  // wait for WiFi connection
  if ((WiFiMulti.run() == WL_CONNECTED)) {

    HTTPClient http;

    Serial.print("[HTTPS] begin...\n");

    http.begin("https://some.secure_server.com/auth/authorise", "2F 2A BB 23 6B 03 89 76 E6 4C B8 36 E4 A6 BF 84 3D DA D3 9F");

    http.addHeader("Content-Type", "application/x-www-form-urlencoded");

    int httpCode = http.POST("user_id=mylogin&user_password=this%20is%20my%20%24ecret%20pa%24%24word");
    if (httpCode > 0) {
      http.writeToStream(&Serial);

      // HTTP header has been send and Server response header has been handled
      Serial.printf("[HTTP] ... code: %d\n", httpCode);

      // file found at server
      if (httpCode == HTTP_CODE_OK) {
        String payload = http.getString();
        Serial.println(payload);
      }
    } else {
      Serial.printf("[HTTP] ... failed, error: %s\n", http.errorToString(httpCode).c_str());
    }
    http.end();
  }

  delay(10000);
}
© www.soinside.com 2019 - 2024. All rights reserved.