How to get the domain of the current page

https://domain.com:8080/xwiki/bin/view/…

I can create url by the function createURL.

But I will add a link like https://domain.com/web/pic/

So I just want to get the domain, such as https://domain.com:8080

I am worried about the change of the domain in the future. If then, it will be ok.

Thanks

If your need is to create a full URL to some page you could use $doc.getExternalURL APIs.