Boost::URL kullanarak C++'ta URL'den host nasıl ayrıştırılır

Şöyle bir URL’niz varsa:

snippet.cpp
std::string myURL = "https://subdomain.example.com/api/test";

Boost::URL kullanarak ondan host adını ayrıştırabilirsiniz (en az Boost 1.81.0 gerekir, önceki boost sürümlerinde Boost.URL yoktur). Şunu kullanarak:

snippet2.cpp
boost::urls::url_view url(myURL);
std::string host = url.host();

std::cout << host << std::endl; // "subdomain.example.com" yazdırır

Tam örnek:

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" yazdırır
}

Şunu kullanarak derleyin:

build_urlhost.sh
g++ -o urlhost urlhost.cpp -lboost_url

Check out similar posts by category: Boost C/C++