Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get domain from a QUrl object
QtWS25 Last Chance

Get domain from a QUrl object

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    silicomancer
    wrote on 7 Mar 2014, 08:13 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      miladmohtashamirad
      wrote on 14 Mar 2022, 03:29 last edited by
      #2

      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

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on 14 Mar 2022, 07:04 last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved