CURL C++ no return
CURL C++ no return
我在C++程序中使用curl,它从网页返回HTML。但我需要它只返回状态代码。我确信返回内容的函数叫做curl_easy_perform()
。
长话短说,我只需要它返回状态代码,而不是内容。
这是我的密码。
CURL *curl = curl_easy_init();
if(curl) {
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "example.com");
res = curl_easy_perform(curl);
if(res == CURLE_OK) {
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
}
curl_easy_cleanup(curl);
}
默认情况下,对于HTTP(S(请求,curl_easy_perform()
将执行GET
请求,该请求检索所请求资源的标头和内容。由于您不需要内容,因此应该发送HEAD
请求,该请求将只检索资源的标头,而不检索其内容。
为此使用CURLOPT_NOBODY
选项:
CURLOPT_NOBODY-在不获取正文的情况下进行下载请求
设置为1的长参数告诉libcurl在进行下载时不要在输出中包含body部分对于HTTP(S(,这使得libcurl执行HEAD请求对于大多数其他协议,这意味着不要求传输身体数据。
例如:
CURL *curl = curl_easy_init();
if(curl) {
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "example.com");
curl_easy_setopt(curl, CURLOPT_NOBODY, 1); // <-- ADD THIS
res = curl_easy_perform(curl);
if(res == CURLE_OK) {
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
}
curl_easy_cleanup(curl);
}
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- c++20[[no.unique_address]]中的新功能是什么
- 以线程安全的方式调用"QQuickPaintedItem::updateImage(const QImage&image)"(no QThread)
- "no matching function for call to 'Vector::Vector'"错误
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- C++函数中的精确"return"矩
- '[](std::list& list)<int>{return std::move(list)}(list)' 是否保证将 'list' 留空?
- 处理"no operator found"
- 方法错误"not all control paths return a value"和方法不返回值
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 如何解决"no Qt platform plugin could be initialized"问题?
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- 为什么当我在 std::string 中打开文件名的 fstream 时出现"no matching function"错误?
- "Warning : No return statement in function returning non-void"是什么意思?
- 如何修复空虚拟方法的"warning: no return statement in function returning non-void [-Wreturn-type]"
- 朋友类中的no return语句
- "single assignment"与"no early return"
- 键入 No return,在函数中返回 non-void