diff --git a/src/Http/HttpClient.cpp b/src/Http/HttpClient.cpp index a3651064..ea39c826 100644 --- a/src/Http/HttpClient.cpp +++ b/src/Http/HttpClient.cpp @@ -264,7 +264,7 @@ ssize_t HttpClient::onRecvHeader(const char *data, size_t len) { _total_body_size = -1; } - if (_total_body_size == 0) { + if (_total_body_size == 0 || _method == "HEAD") { // 后续没content,本次http请求结束 [AUTO-TRANSLATED:8532172f] // There is no content afterwards, this http request ends onResponseCompleted_l(SockException(Err_success, "The request is successful but has no body")); diff --git a/tests/test_http_head.cpp b/tests/test_http_head.cpp new file mode 100644 index 00000000..385bc372 --- /dev/null +++ b/tests/test_http_head.cpp @@ -0,0 +1,27 @@ +#include "Http/HttpRequester.h" + +int main() { + auto requester = std::make_shared(); + requester->setMethod("HEAD"); + + requester->startRequester( + "http://baidu.com", + + [](const toolkit::SockException &ex, const mediakit::Parser &parser) { + if (ex) { + PrintI("HEAD请求失败: %s", ex.what()); + return; + } + + // 检查HTTP状态码 + if (parser.status() != "200") { + PrintI("HEAD请求返回错误状态: %s", parser.status().c_str()); + return; + } + for (auto &header : parser.getHeader()) { + PrintI("key=%s, val=%s", header.first.c_str(), header.second.c_str()); + } + }); + getchar(); + return 0; +} \ No newline at end of file