14

I have got URL as referrer and want to get protocol and domain from it.

For example: If URL is https://test.domain.com/a/b/c.html?test=hello then output needs to be https://test.domain.com. I have gone through http://docs.oracle.com/javase/7/docs/api/java/net/URI.html and I can't seems to find any method that can directly do so.


I am not using Spring, so can't use Sprint classes (if any).


Guys I can write custom login to get port, domain and protocol from URL, but looking for API which already has this implemented and can minimize my time in testing various scenarios.

11

3 Answers 3

23

To elaborate on what @Rupesh mentioned in @mthmulders answer,

getAuthority() gives both domain and port. So you just concatenate it with getProtocol() as prefix:

URL url = new URL("https://test.domain.com/a/b/c.html?test=hello");
String protocol = url.getProtocol();
String authority = url.getAuthority();
return String.format("%s://%s", protocol, authority);
1
  • If someone like me just want the URL without host name and port then use url.getPath()
    – Kapil
    Commented Jan 20, 2020 at 10:21
15

Create a new URL object using your String value and call getHost() or any other method on it, like so:

URL url = new URL("https://test.domain.com/a/b/c.html?test=hello");
String protocol = url.getProtocol();
String host = url.getHost();
int port = url.getPort();

// if the port is not explicitly specified in the input, it will be -1.
if (port == -1) {
    return String.format("%s://%s", protocol, host);
} else {
    return String.format("%s://%s:%d", protocol, host, port);
}
4
  • this will not work if url is https://test.domain.com:80/a/b/c.html?test=hello the output should be https://test.domain.com:80. I can write these logic but looking for tried and tested API
    – Rupesh
    Commented Aug 20, 2015 at 9:20
  • 1
    port check is not needed, getAuthority gives domain and port
    – Rupesh
    Commented Aug 20, 2015 at 9:48
  • 1
    you must change if condition : if (port == -1) Commented Sep 4, 2017 at 13:16
  • Worked for me. But instead of this multi line approach, I prefer getAuthority() which does the same job in single line.
    – vinsinraw
    Commented Jan 14, 2020 at 6:17
-1

getAuthority() returns the host along with the port but getHost() returns only the host name. So if URL is "https://www.hello.world.com:80/x/y/z.html?test=hello", then getAuthority() return www.hello.world.com:80 while getHost() returns www.hello.world.com

Example

URL url = new URL("https://www.hello.world.com:80/x/y/z.html?test=hello");
String protocol = url.getProtocol();
String host = url.getHost();
int port = url.getPort();
String authority = url.getAuthority();

System.out.println("Host "+host);   // www.hello.world.com
System.out.println("authority "+authority);   // www.hello.world.com:80

//Determining Protocol plus host plus port (if any) url using Authority (simple single line step)
System.out.println("ProtocolHostPortURL:: "+ String.format("%s://%s", protocol, authority));

//Determining Protocol plus host plus port (if any) url using Authority (multi line step)
//If the port is not explicitly specified in the input, it will be -1.
if (port == -1) {
    System.out.println("ProtocolHostURL1:: "+ String.format("%s://%s", protocol, host));        
} else {
    System.out.println("ProtocolHostPortURL2:: "+ String.format("%s://%s:%d", protocol, host, port));    
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.