diff --git a/src/Http/HttpClient.cpp b/src/Http/HttpClient.cpp index d0cc9dc9..45ef2790 100644 --- a/src/Http/HttpClient.cpp +++ b/src/Http/HttpClient.cpp @@ -41,6 +41,8 @@ void HttpClient::sendRequest(const string &url, float timeout_sec, float recv_ti if (_path.empty()) { _path = "/"; } + //重新设置header,防止上次请求的header干扰 + _header = _user_set_header; auto pos = host.find('@'); if (pos != string::npos) { //去除?后面的字符串 @@ -94,6 +96,7 @@ void HttpClient::sendRequest(const string &url, float timeout_sec, float recv_ti void HttpClient::clear() { _url.clear(); _header.clear(); + _user_set_header.clear(); _body.reset(); _method.clear(); _path.clear(); @@ -116,14 +119,14 @@ void HttpClient::setMethod(string method) { } void HttpClient::setHeader(HttpHeader header) { - _header = std::move(header); + _user_set_header = std::move(header); } HttpClient &HttpClient::addHeader(string key, string val, bool force) { if (!force) { - _header.emplace(std::move(key), std::move(val)); + _user_set_header.emplace(std::move(key), std::move(val)); } else { - _header[std::move(key)] = std::move(val); + _user_set_header[std::move(key)] = std::move(val); } return *this; } diff --git a/src/Http/HttpClient.h b/src/Http/HttpClient.h index 01ac795a..1f0b35ee 100644 --- a/src/Http/HttpClient.h +++ b/src/Http/HttpClient.h @@ -181,6 +181,7 @@ private: bool _complete = false; string _url; HttpHeader _header; + HttpHeader _user_set_header; HttpBody::Ptr _body; string _method; string _path;