Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract base url from full url

Tags:

android

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 string parsing - but wanted to know if there is something in Uri where I can get it directly.

like image 609
bschandramohan Avatar asked Dec 20 '10 19:12

bschandramohan


People also ask

How do I find the URL of a base URL?

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.

What is the base URL of a server?

The Server Base URL is the URL via which users access Confluence. The base URL must be set to the same URL by which browsers will be viewing your Confluence site.

What is meant by base URL?

The URL found in the address bar of the front page of a website is its base URL. In other words, the common prefix found while navigating inside a given website is known as the base URL. One can select a base URL from the list of those available with help of the URL general properties page.


1 Answers

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

like image 98
Pascal Avatar answered Oct 20 '22 02:10

Pascal