Extract base url from full url

前端 未结 3 1961
小鲜肉
小鲜肉 2021-02-08 10:42

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

I can always do strin

相关标签:
3条回答
  • 2021-02-08 11:13

    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
    }
    
    0 讨论(0)
  • 2021-02-08 11:14

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

    Preliminary note:

    One should pay attention to this important note from the URL javadoc page:

    "Note, the URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use URI, and to convert between these two classes using toURI() and URI.toURL()."

    Self Explanatory Unit tests:

    Here are 2 simlple unit tests to illustrate the 'base Url' concept

    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));
        }
    }
    

    Extracting base URL:

    So, "extracting base Url from full url" can be done like this:

    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 that (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;
        }
    }
    

    Note:
    Remove the trailing "/" is you don't want it

    0 讨论(0)
  • 2021-02-08 11:22

    There's nothing in the Android URI class that gives you the base URL directly- As gnuf suggests, you'd have to construct it as the protocol + getHost().

    The string parsing way might be easier and let you avoid stuffing everything in a try/catch block. Just take a substring of the URL, everything from the beginning of the string to the third "/" symbol.

    0 讨论(0)
提交回复
热议问题