Get domain from a QUrl object
-
wrote on 7 Mar 2014, 08:13 last edited by
Seemed an easy task... but I could not find a way to get the domain part of a URL.
Lets say my URL ist "http://www.example.com/dir/index.html". I can easily get e.g. the TLD part (".com") and I can get the host ("www.example.com"). However I need to find out which domain is used ("example.com").
Did I overlook something or am I right that QUrl can not provide this?
-
wrote on 14 Mar 2022, 03:29 last edited by
you can use QUrl. it extracts the domain:
QString url("https://somedomain.com/index/of/somepage/blah/blah"); QUrl qu(url); qDebug() << "qu.host " << qu.host();
it gives you: somedomain.com
-
wrote on 14 Mar 2022, 07:04 last edited by
A URL contains a "host" that may, or may not, be a dotted DNS name. It could be a dotted quad IP address, or any other valid representation (e.g. http://www.google.com, http://142.250.71.78, http://0x8efa474e, http://2398766926, or an IPv6 address). It would be unwise to assume that a fully-qualified DNS domain name (FQDN) was present in the "host" string. You can do a reverse DNS lookup to get a DNS name (or multiple) for the numeric host values, if one is available. (QHostAddress and QHostInfo may be useful).
Even if you have a fully-qualified DNS name the "domain" is tricky. www.google.com => google.com seems straightforward. What do you define as the domain if the host is only "google.com" or if it is "www.google.com.au" or "google.co.nz"? How about "xyzzy.dev.mycompany.co.uk"?
In a rough approximation, the domain is everything after the first dot in a FQDN where there is more than one dot, and the whole string otherwise. QString and QRegularExpression should be useful here.