如何使用NODEMCU发送HTTPS GET请求 [英] How to send HTTPS GET request using NODEMCU

查看:34
本文介绍了如何使用NODEMCU发送HTTPS GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Nodemcu-Arduino代码发送HTTPS-GET/POST请求。

我花了几天时间寻找使用HTTPS协议向网站发送GET请求的工作示例,但我找到的所有示例都不成功。

我希望它也能帮助其他人!

推荐答案

Here is the link that helped me

若要使示例与HTTPS协议一起工作,您必须使用WiFiClientSecure库并调用您拥有的WiFiClientSecure对象的client.setInsecure()函数。

以下是一个完全正常工作的代码:

#include <ESP8266WiFi.h>

const char* ssid     = "WIFI_SSID";
const char* password = "WIFI_PASS";

const char* host = "DOMAIN_NAME"; // only google.com not https://google.com

void setup() {
  Serial.begin(115200);
  delay(10);

  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  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());
}

void loop() {
  delay(5000);

  Serial.print("connecting to ");
  Serial.println(host);

  // Use WiFiClient class to create TCP connections
  WiFiClientSecure client;
  const int httpPort = 443; // 80 is for HTTP / 443 is for HTTPS!
  
  client.setInsecure(); // this is the magical line that makes everything work
  
  if (!client.connect(host, httpPort)) { //works!
    Serial.println("connection failed");
    return;
  }

  // We now create a URI for the request
  String url = "/arduino.php";
  url += "?data=";
  url += "aaaa";


  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1
" +
               "Host: " + host + "
" + 
               "Connection: close

");

  Serial.println();
  Serial.println("closing connection");
}

这篇关于如何使用NODEMCU发送HTTPS GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆