Arduino - 关于ESP8266HTTPClient 的 Authorization问题

文件路径(C:\Users\ ~~你的电脑名称~~ \AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\libraries\ESP8266HTTPClient\src)
ESP8266HTTPClient.cpp中:

bool HTTPClient::sendHeader(const char * type)
{
    if(!connected()) {
        return false;
    }

    String header = String(type) + " " + (_uri.length() ? _uri : F("/")) + F(" HTTP/1.");

    if(_useHTTP10) {
        header += "0";
    } else {
        header += "1";
    }

    header += String(F("\r\nHost: ")) + _host;
    if (_port != 80 && _port != 443)
    {
        header += ':';
        header += String(_port);
    }
    header += String(F("\r\nUser-Agent: ")) + _userAgent +
              F("\r\nConnection: ");

    if(_reuse) {
        header += F("keep-alive");
    } else {
        header += F("close");
    }
    header += "\r\n";

    if(!_useHTTP10) {
        header += F("Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0\r\n");
    }

    if(_base64Authorization.length()) {
        _base64Authorization.replace("\n", "");
        header += F("Authorization: Basic ");//大约在1121行
        header += _base64Authorization;
        header += "\r\n";
    }

    header += _headers + "\r\n";

    Serial.printf("[HTTP-Client] sending request header\n-----\n%s-----\n", header.c_str());

    return (_client->write((const uint8_t *) header.c_str(), header.length()) == header.length());
}

当HTTP请求时,需要加入 Authorization的请求头时会加多Basic导致出错,删除即可
header += F("Authorization: Basic ");//大约在源码1121行
不删除

GET http://ota.heclouds.com/ota/south/check?dev_id=****&manuf=101&model=10101&type=2&version=1 HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Authorization: Basic version=2018-10-31&res=products%2F******&et=1561476160&method=sha1&sign=***********
Host: ota.heclouds.com

删除后

GET http://ota.heclouds.com/ota/south/check?dev_id=****&manuf=101&model=10101&type=2&version=1 HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Authorization: version=2018-10-31&res=products%2F******&et=1561476160&method=sha1&sign=***********
Host: ota.heclouds.com

效果看倒数第二行 Authorization 字段
http.end();会清空之前获取的所有数据,请在处理完再调用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容