如何使用 libcurl 在本地 IP 上获取 HTML?

How can I get an HTML on a local IP using libcurl?

本文关键字:获取 HTML IP 何使用 libcurl      更新时间:2023-10-16

在工作中,我在建筑物的另一部分有一个设备,它托管一个网页:我需要解析的网页:http://10.1.1.165/

我正在尝试使用 curlpp 来检索页面,然后使用 libxml2 来解析 html。

目前我有:

curlpp::Easy request;
request.setOpt(curlpp::options::Url(std::string("http://10.1.1.165/")));
std::list<std::string> headers;
headers.push_back(HEADER_ACCEPT);
headers.push_back(HEADER_USER_AGENT);
std::ostringstream responseStream;
curlpp::options::WriteStream streamWriter(&responseStream);
request.setOpt(streamWriter);
request.perform();
std::string re = responseStream.str();
htmlDoc = htmlReadDoc((xmlChar*)re.c_str(), NULL, NULL, HTML_PARSE_RECOVER | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING);

代码中断在行request.peform();输出:

terminate called after throwing an instance of 'curlpp::LibcurlRuntimeError'
what():  No URL set!

我很困惑,因为我遵循了 curlpp 示例代码和此处提供的示例中完全相同的说明:https://blog.laplante.io/2014/11/parsing-html-c-revisited/

我是否忘记设置某些设置或错误地传递了 url?

我得出的结论是libcurl的C++包装器被破坏了。执行 curlpp::Easy request(( 不起作用。我使用了 C 版本,一切正常:将 HTML 源代码读取为字符串