I have a URL like http://hostname:port_no/control/login.jsp
.
I have the above url stored in some String.Now, I need to extract hostname
from the String.
I am doing like this in my Java code
String domain = url.substring(url.indexOf('/') + 2, url.lastIndexOf(':'));
I want to know if there is any better way to do the same.
You can use the java.net.URI
-class to extract the hostname from the string.
Here below is a method from which you can extract your hostname from a string.
public String getHostName(String url) {
URI uri = new URI(url);
String hostname = uri.getHost();
// to provide faultproof result, check if not null then return only hostname, without www.
if (hostname != null) {
return hostname.startsWith("www.") ? hostname.substring(4) : hostname;
}
return hostname;
}
This above gives you the hostname, and is faultproof if your hostname does start with either hostname.com/...
or www.hostname.com/...
, which will return with 'hostname'.
If the given url
is invalid (undefined hostname), it returns with null.
java.net.URL aURL;
try {
aURL = new java.net.URL("http://example.com:80/docs/");
System.out.println("host = " + aURL.getHost()); //example.com
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.net.URL u = new URL("http://hostname:port_no/control/login.jsp");
System.err.println(u.getHost());
If you want string work, then try the following code sample,
String URL= "http://hostname:port_no/control/login.jsp";
String s_URL[] = ULR.split("//");
String s1 = s_URL[1];
String s2[] = s1.split(":");
String hostname = s2[0];
In Java:
String hostname = url.split("://")[1].split(":")[0];
String portnumber = url.split("://")[1].split(":")[1].split("/")[0];
Hope this helps.
@KarelG's answer is the best answer, though I had specific issues with certain non-standard domains. The example issue is self contained below.
For certain "real world" input values, I had to add a check to the URI scheme to avoid mis-parsing of some addresses. This is the changed code.
import java.net.URI;
import java.net.*;
public class Domain {
public static String getDomainName(String url) {
try {
URI uri = new URI(url);
String domain = uri.getHost();
System.out.println("domain: " + domain);
if (uri.getScheme() != null) {
return domain.startsWith("www.") ? domain.substring(4) : domain;
} else {
return uri.getSchemeSpecificPart();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
Below is the test case and value that was failing.
Domain.java
javac Domain.java
java Domain
import java.net.URI;
import java.net.*;
import java.io.*;
public class Domain {
public static String longname = "https://www.3dprintingmedia.network/longform/page.html";
public static String name = "www.3dprintingmedia.network";
public static void getDomain(String url) {
try {
URI uri = new URI(url);
String domain = uri.getHost();
System.out.println("protocol: " + uri.getScheme());
System.out.println("path: " + uri.getPath());
System.out.println("name: " + name);
System.out.println("domain: " + domain);
System.out.println(domain.startsWith("www.") ? domain.substring(4) : domain);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("Parsing domain: " + name);
getDomain(longname);
getDomain(name);
System.exit(0);
}
}