Friday, September 13, 2013

Networking : Lesson 02 - URL class


Networking : Lesson 02 - URL class

URL contains :
          Protocol                                    : http
          Server Name / IP address     : localhost
          Port Number                            : 8080
          File Name / Directory Name : NetworkExample/index.html


package Networking;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class URLExample {

     /**
     * @param args
     */
     public static void main(String[] args) {

          try {
                URL url = new URL("http://educationjavacode.blogspot.com/search/label/Networking%20%3A%20Lesson%2001%20%E2%80%93%20Socket%20Programming");

                System.out.println("Authority : " + url.getAuthority());
                System.out.println("DefaultPort : " + url.getDefaultPort());
                System.out.println("File : " + url.getFile());
                System.out.println("Host : " + url.getHost());
                System.out.println("Path : " + url.getPath());
                System.out.println("Port : " + url.getPort());
                System.out.println("Protocol : " + url.getProtocol());
                System.out.println("Query : " + url.getQuery());
                System.out.println("Ref : " + url.getRef());
                System.out.println("UserInfo : " + url.getUserInfo());
                System.out.println("Class : " + url.getClass());
                System.out.println("Content : " + url.getContent());
         } catch (MalformedURLException e) {

                e.printStackTrace();
         } catch (IOException e) {
                e.printStackTrace();
         }
     }
}


Output :

Authority : educationjavacode.blogspot.com
DefaultPort : 80
File : /search/label/Networking%20%3A%20Lesson%2001%20%E2%80%93%20Socket%20Programming
Host : educationjavacode.blogspot.com
Path : /search/label/Networking%20%3A%20Lesson%2001%20%E2%80%93%20Socket%20Programming
Port : -1
Protocol : http
Query : null
Ref : null
UserInfo : null
Class : class java.net.URL
Content : sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@11b9fb1