开发者

Extract base url from full url

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.


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
}


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜