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

时间:2017-08-08 02:15:32

标签: esp8266 arduino-esp8266

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

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

1 个答案:

答案 0 :(得分:0)

这是很晚的方法,但是这篇文章是google返回的文章之一,因此,这是一种帮助像我这样的人的好方法。

事实证明,有关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);
}