如何在 C++ 中使用 Boost::URL 从 URL 解析主机名
如果你有如下 URL:
snippet.cpp
std::string myURL = "https://subdomain.example.com/api/test";你可以使用 Boost::URL 从中解析主机名(你需要至少 Boost 1.81.0,之前的 boost 版本没有 Boost.URL)
snippet2.cpp
boost::urls::url_view url(myURL);
std::string host = url.host();
std::cout << host << std::endl; // 打印 "subdomain.example.com"
完整示例:
urlhost.cpp
#include <string>
#include <iostream>
#include <boost/url.hpp>
int main() {
std::string myURL = "https://subdomain.example.com/api/test";
boost::urls::url_view url(myURL);
std::string host = url.host();
std::cout << host << std::endl; // 打印 "subdomain.example.com"
}使用以下命令编译
build_urlhost.sh
g++ -o urlhost urlhost.cpp -lboost_urlIf this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow