feature:添加http head请求的支持 (#4321)
Some checks failed
Android / build (push) Has been cancelled
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Docker / build (push) Has been cancelled
Linux / build (push) Has been cancelled
macOS / build (push) Has been cancelled
Windows / build (push) Has been cancelled

添加http head请求的支持
This commit is contained in:
肖锋 2025-06-30 20:46:18 +08:00 committed by GitHub
parent 48fd240817
commit fd34b5526b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 1 deletions

View File

@ -264,7 +264,7 @@ ssize_t HttpClient::onRecvHeader(const char *data, size_t len) {
_total_body_size = -1; _total_body_size = -1;
} }
if (_total_body_size == 0) { if (_total_body_size == 0 || _method == "HEAD") {
// 后续没content本次http请求结束 [AUTO-TRANSLATED:8532172f] // 后续没content本次http请求结束 [AUTO-TRANSLATED:8532172f]
// There is no content afterwards, this http request ends // There is no content afterwards, this http request ends
onResponseCompleted_l(SockException(Err_success, "The request is successful but has no body")); onResponseCompleted_l(SockException(Err_success, "The request is successful but has no body"));

27
tests/test_http_head.cpp Normal file
View File

@ -0,0 +1,27 @@
#include "Http/HttpRequester.h"
int main() {
auto requester = std::make_shared<mediakit::HttpRequester>();
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;
}