Parsing a URL - Java Network

Java examples for Network:URL

Description

Parsing a URL

Demo Code

import java.net.MalformedURLException;
import java.net.URL;

public class Main {
  public static void main(String[] argv) {
    try {/*from ww  w.ja  v  a  2 s .  com*/
      URL url = new URL("http://hostname:80/index.html#_top_");

      String protocol = url.getProtocol(); // http
      String host = url.getHost(); // hostname
      int port = url.getPort(); // 80
      String file = url.getFile(); // index.html
      String ref = url.getRef(); // _top_
    } catch (MalformedURLException e) {
    }
  }
}

Related Tutorials