Extract base url from full url

Any quick way to extract base url from full url? for example, if I have http://test.example.com/abcd/test.html - I want only http://test.example.com .

I can always parse strings, but I wanted to know if there is anything in Uri where I can get it directly.

+4
source share
3 answers

What about:

import java.net.URL; import java.net.MalformedURLException; try { URL url = new URL("http://test.example.com/abcd/test.html"); String baseUrl = url.getProtocol() + "://" + url.getHost(); } catch (MalformedURLException e) { // do something } 
+21
source

java.net.URL doc can help you figure out how to "Extract base url from full URL"

Preliminary Note:

You should pay attention to this important note on the javadoc URL page :

"Note that in certain cases, the URI class escapes its component fields. The recommended way to control the encoding and decryption of URLs is to use URIs and convert between the two classes using toURI () and URI.toURL () ."

Self-evident unit tests:

Here are two simple test cases illustrating the concept of basic Url.

 package com.elementique.web; import org.junit.Test; import java.net.URI; import java.net.URL; import static org.junit.Assert.assertEquals; public class UrlTest { @Test public void baseUrlAuthority() throws Exception { URL url = URI.create("http://username: password@host1 :8080/directory/file?query#ref").toURL(); assertEquals("http", url.getProtocol()); // protocol is also known as 'scheme' assertEquals("username: password@host1 :8080", url.getAuthority()); String baseUrlAuthority= url.getProtocol() + "://" + url.getAuthority(); assert (url.toString().startsWith(baseUrlAuthority)); } @Test public void baseUrlHostAndDefaultPort() throws Exception { URL url = URI.create("http://host2/a/b/c").toURL(); assertEquals(-1, url.getPort()); // -1 as port is not defined in this case String baseUrlHostAndDefaultPort= url.getProtocol() + "://" + url.getHost(); assert (url.toString().startsWith(baseUrlHostAndDefaultPort)); } } 

Extract base URL:

So, "fetching the base url from the full URL" can be done as follows:

 return url.getProtocol()+"://"+url.getAuthority()+"/" 

Or this if you do NOT want the username / password part

 if(url.getPort() == -1){ // port is not return url.getProtocol()+"://"+url.getHost()+"/"; } else { return url.getProtocol()+"://"+url.getHost()+":"+url.getPort()"/"; } 

Implementation:

Here is an implementation that does this (based on getAuthority ()):

 public static String getBaseUrl(String urlString) { if(urlString == null) { return null; } try { URL url = URI.create(urlString).toURL(); return url.getProtocol() + "://" + url.getAuthority() + "/"; } catch (Exception e) { return null; } } 

Remarks:
Delete the trailing "/", you do not want it

+3
source

There is nothing in the URI class for Android that would give you the base URL. As gnuf suggests, you will need to build it as a protocol + getHost ().

The line parsing method can be simpler and allows you not to type everything into a try / catch block. Just take the substring of the URL, everything from the beginning of the line to the third "/" character.

+1
source

Source: https://habr.com/ru/post/1332809/


All Articles