You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ESP32使用HTTPClient发起带自定义Header的HTTPS请求问题

解决方案:ESP32 Arduino实现带自定义Header的HTTPS请求

一、标准Arduino ESP32库直接实现(无需额外库)

你不需要切换到beginRequest()就能添加自定义Header,原代码里的sendHeader()调用位置有误——只要在GET()方法之前调用sendHeader()即可,HTTPClient在begin(client, url)初始化后完全支持添加自定义请求头。

修改后的代码示例

WiFiClientSecure client;
client.setInsecure(); // 测试阶段跳过证书校验

HTTPClient https;
https.useHTTP10(true);
Serial.println("https.begin...");
if (https.begin(client, "https://path.to.binary.file")) {  // 初始化HTTPS连接
    Serial.println("添加自定义Header并发送GET请求...");
    // 在这里添加自定义Header,每个Header调用一次sendHeader
    https.sendHeader("X-device", "12345678"); // 注意格式:第一个参数是Header名,第二个是值
    https.sendHeader("Another-Header", "custom-value"); // 可添加多个

    // 发送请求并获取响应码
    int httpCode = https.GET();
    Serial.printf("Response code: %u\n", httpCode);
    Serial.printf("Content length: %u\n", https.getSize());

    // 读取二进制数据(可修改为写入SPIFFS)
    uint32_t bytesRead = 0;
    uint8_t b;
    if (httpCode == HTTP_CODE_OK) {
        while (client.connected()) {
            while (client.available()) {
                ++bytesRead;
                b = client.read();
                Serial.printf("0x%02x ", b);
                if ((bytesRead % 16) == 0) {
                    Serial.println();
                }
                // 写入SPIFFS示例:file.write(b);
            }
        }
    }
    Serial.printf("\nBytes read: %u\n", bytesRead);
    https.end();
} else {
    Serial.println("无法连接到服务器");
}

若需使用beginRequest()构造复杂请求

如果你需要更灵活的请求构造(比如自定义请求方法、分块发送等),可以先让WiFiClientSecure建立连接,再用HTTPClient::beginRequest()的重载方法,指定https=true:

WiFiClientSecure client;
client.setInsecure();
const char* host = "path.to.binary.file";
const uint16_t port = 443;
const char* path = "/your/binary/path";

// 先建立SSL连接
if (!client.connect(host, port)) {
    Serial.println("SSL连接失败");
    return;
}

HTTPClient https;
// 使用beginRequest重载,传入client、host、port、path,最后指定https=true
if (https.beginRequest(client, host, port, path, true)) {
    https.sendHeader("X-device", "12345678");
    https.sendHeader("Accept", "*/*");
    https.sendRequest("GET"); // 指定请求方法

    int httpCode = https.getResponseCode();
    Serial.printf("Response code: %u\n", httpCode);

    // 读取响应数据逻辑同前
    uint32_t bytesRead = 0;
    uint8_t b;
    if (httpCode == HTTP_CODE_OK) {
        while (client.connected()) {
            while (client.available()) {
                ++bytesRead;
                b = client.read();
                Serial.printf("0x%02x ", b);
                if ((bytesRead % 16) == 0) {
                    Serial.println();
                }
            }
        }
    }
    Serial.printf("\nBytes read: %u\n", bytesRead);
    https.endRequest();
    client.stop();
}

二、替代库推荐

如果标准库的灵活性仍不能满足需求,可以使用:

  • ESPAsyncHTTPClient:支持异步HTTPS请求,内置对自定义Header的支持,适合需要非阻塞操作的场景,使用方式与HTTPClient类似但更高效。
  • WebClient:另一个轻量级HTTP/HTTPS客户端库,支持自定义Header和多种请求方法。

内容的提问来源于stack exchange,提问作者Scott Porter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 01:57:38