Sample code for 30+ languages & platforms
Java

Parse a URL into its Component Parts

See more HTTP Examples

Demonstrates how to parse a URL into it's component parts.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Url;

public class ChilkatExample {

    public static void main(String[] args) {
        Url url = new Url();

        String urlStr = "https://www.amazon.com/Anarchy-State-Utopia-Robert-Nozick/dp/0465051006/ref=sr_1_1?s=books&ie=UTF8&qid=1430344305&sr=1-1&keywords=nozick#frag123";

        url.parseUrl(urlStr);
        // Assume success..

        System.out.println("URL: " + urlStr);
        System.out.println("Host: " + url.getHost());
        System.out.println("Port: " + url.getPort());
        System.out.println("HostType: " + url.getHostType());
        System.out.println("Ssl: " + url.getSsl());
        System.out.println("Path: " + url.getPath());
        System.out.println("Query: " + url.getQuery());
        System.out.println("Frag: " + url.getFrag());
        System.out.println("----");

        urlStr = "http://matt:secret@www.chilkatsoft.com:8080/somepath.asp?test=123&size=2";

        url.parseUrl(urlStr);
        // Assume success..

        System.out.println("URL: " + urlStr);
        System.out.println("Host: " + url.getHost());
        System.out.println("Port: " + url.getPort());
        System.out.println("HostType: " + url.getHostType());
        System.out.println("Ssl: " + url.getSsl());
        System.out.println("Login: " + url.getLogin());
        System.out.println("Password: " + url.getPassword());
        System.out.println("Path: " + url.getPath());
        System.out.println("Query: " + url.getQuery());
        System.out.println("Frag: " + url.getFrag());
    }
}