Friday, March 11, 2016

Java Network Example : How To Get URL Properties

Intro


This example, will demonstrates how to get URL properties such as URL port, URL host, URL path, and etc using Java Network programming.

Example


package com.freesamplecode.java.networking;

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

public class GetURLPropertiesDemo {
 public static void main(String[] args){
  URL url = null;
  
  try {
   url = new URL("http://free-samplecode.blogspot.com/p/java.html");
   System.out.println("Protocol : "+url.getProtocol());
   System.out.println("Host : "+url.getHost());
   System.out.println("Port : "+url.getPort());
   System.out.println("Query : "+url.getQuery());
   System.out.println("Path : "+url.getPath());
   System.out.println("User Info : "+url.getUserInfo());
  } catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }
}


Output


Protocol : http
Host : free-samplecode.blogspot.com
Port : -1
Query : null
Path : /p/java.html
User Info : null

Screenshot

How To Get URL Properties In Java

Related Posts:

0 comments: