Java Utililty Methods Host Name Get

List of utility methods to do Host Name Get

Description

The list of methods to do Host Name Get are organized into topic(s).

Method

StringgetHostname()
get Hostname
String retHost = "UNKNOWN_HOST";
try {
    String host = InetAddress.getLocalHost().getCanonicalHostName();
    if (host != null) {
        int idx = host.indexOf(".");
        if (idx > -1) {
            retHost = host.substring(0, idx);
            try {
...
StringgetHostName(Proxy proxy)
get Host Name
InetSocketAddress address = (InetSocketAddress) proxy.address();
return address.getHostName();
StringgetHostName(String destination)
get Host Name
return new URL(destination).getHost();
StringgetHostName(String host)
get Host Name
return InetAddress.getByName(host).getCanonicalHostName();
StringgetHostname(String targetEndpoint)
get Hostname
try {
    URI uri = new URI(targetEndpoint);
    return uri.getHost();
} catch (Exception e) {
    return "localhost";
StringgetHostNameByInet()
Uses InetAddress.getLocalHost().getCanonicalHostName() to get the host name.
try {
    String inetHost = InetAddress.getLocalHost().getCanonicalHostName();
    if (inetHost == null || inetHost.trim().isEmpty() || "localhost".equalsIgnoreCase(inetHost.trim()))
        return null;
    return inetHost.trim();
} catch (Exception x) {
    return null;
StringgetHostNameByNic()
Iterates through the found NICs, extracting the host name if the NIC is not the loopback interface.
try {
    for (Enumeration<NetworkInterface> nicEnum = NetworkInterface.getNetworkInterfaces(); nicEnum
            .hasMoreElements();) {
        NetworkInterface nic = nicEnum.nextElement();
        if (nic != null && nic.isUp() && !nic.isLoopback()) {
            for (Enumeration<InetAddress> nicAddr = nic.getInetAddresses(); nicAddr.hasMoreElements();) {
                InetAddress addr = nicAddr.nextElement();
                String chost = addr.getCanonicalHostName();
...
StringgetHtmlSource(final String host)
get Html Source
final StringBuilder sb = new StringBuilder();
InputStream is;
try {
    final URL url = new URL(host);
    is = url.openStream();
    final BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line;
    while ((line = br.readLine()) != null) {
...
StringgetInetHost()
get Inet Host
try {
    return Iterables.getFirst(DOT_SPLITTER.split(InetAddress.getLocalHost().getHostName()), "UNKNOWN_HOST");
} catch (Exception e) {
    return "UNKNOWN_HOST";
StringgetLedgerAllocatorPoolName(int serverRegionId, int shardId, boolean useHostname)
Retrieve the ledger allocator pool name.
if (useHostname) {
    return String.format("allocator_%04d_%s", serverRegionId, InetAddress.getLocalHost().getHostAddress());
} else {
    return String.format("allocator_%04d_%010d", serverRegionId, shardId);