How to Parse URL in Java

What is URL? URL represents Uniform Resource Locator. It's a pointer to a "resource" on the WorldWide Web.
A resource can be something as simple as a file or a directory,or it can be a reference to a more complicated object,such as a query to a database or to a search engine.

A URL can optionally specify a "port", which is the port number to which the TCP connection is made on the remote host machine. If the port is not specified, the default port for the protocol is used instead. For example, the default port for http is 80.

Java Class java.net.URL provides number of methods such as getFile(),getHost(),getQuery() etc to deal with URL. Please refer to J2SE 6 Documentation for URL Class.

Here I will show you how You can retrieve different parts of URL using Java Program.


package com.anuj.network;

import java.net.InetAddress;
import java.net.URL;

public class ParseURL {
    public static void main(String[] args) throws Exception {
        System.out.println(InetAddress.getLocalHost().getHostAddress());
        String pagename = "";
        String str = "http://java.sun.com:80/docs/books/tutorial/index.html?name=networking#DOWNLOADING";

        URL url = new URL(str);
        System.out.println("protocol = " + url.getProtocol());
        System.out.println("authority = " + url.getAuthority());
        System.out.println("host = " + url.getHost());
        System.out.println("port = " + url.getPort());
        System.out.println("path = " + url.getPath());
        System.out.println("query = " + url.getQuery());
        System.out.println("filename = " + url.getFile());
        System.out.println("ref = " + url.getRef());

        int lastslash = str.lastIndexOf("/");
        System.out.println(lastslash);
        if (str.indexOf("?") == -1) {
            pagename = str.substring(lastslash + 1, str.length());
            System.out.println(pagename);
        } else {
            int qnindex = str.indexOf("?");
            pagename = str.substring(lastslash + 1, qnindex);
            System.out.println(pagename);
        }
    }
}
Output :
 10.27.63.58
protocol = http
authority = java.sun.com:80
host = java.sun.com
port = 80
path = /docs/books/tutorial/index.html
query = name=networking
filename = /docs/books/tutorial/index.html?name=networking
ref = DOWNLOADING
42
index.html

No comments:

Post a Comment