Sunday, March 27, 2016

Java Networking Examples : How To Get HTTP Header Information of URL

Intro

This examples will demonstrates how to get header information of URL such as status code, response code, content type, and etc.

To get a header information of URL, you can use HttpURLConnection.getHeaderFields() method.

Examples


package com.freesamplecode.java.networking;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class GetHeaderInformationURLDemo {
 public static void main(String[] args){
  
  try {
   //create URL object
   URL url = new URL("http://free-samplecode.blogspot.com/p/java.html");
   
   //open URL connection
   HttpURLConnection httpUrlConnection =  (HttpURLConnection) url.openConnection();
   
   //get all URL header informations
   Map headerField = httpUrlConnection.getHeaderFields();
   
   Set set = headerField.entrySet();
   
   Iterator it = set.iterator();
   
   while(it.hasNext()){
    System.out.println(it.next());
   }
   

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

Output


null=[HTTP/1.1 200 OK]
Expires=[Sun, 27 Mar 2016 13:31:17 GMT]
X-XSS-Protection=[1; mode=block]
Last-Modified=[Sun, 27 Mar 2016 13:15:17 GMT]
Connection=[Keep-Alive]
Server=[GSE]
X-Content-Type-Options=[nosniff]
Cache-Control=[private, max-age=0]
Date=[Sun, 27 Mar 2016 13:31:17 GMT]
Vary=[Accept-Encoding]
Transfer-Encoding=[chunked]
Content-Type=[text/html; charset=UTF-8]
Accept-Ranges=[none]

Screenshot

How To Get HTTP Header Information of URL In Java

Java Networking Examples : How To Display Source Code Of A Web Page Using URLConnection

Intro

This examples will demonstrates how to display source code of a web page using URLConnection class.

Examples


package com.freesamplecode.java.networking;

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

public class GetSourceCodeWebDemo {
 public static void main(String[] args){
  try {
   
   //create URL object
   URL url = new URL("http://free-samplecode.blogspot.com/p/java.html");
   
   //open URL connection
   URLConnection urlConnection = url.openConnection();
   
   //get stream of URL 
   InputStream is = urlConnection.getInputStream();
   int i = 0;
   while((i = is.read()) != -1){
    System.out.print((char)i);
   }
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }
}

Output

How To Display Source Code Of A Web Page Using URLConnection

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

Java Network Example : How To Get Contents Of The URL

Intro


This examples, will demonstrates how to get content of the URL using Java program. The contents are in the HTML format.

Example


package com.freesamplecode.java.networking;

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

public class GetContentURLDemo {
 public static void main(String[] args){
  URL url = null;
  InputStream is = null;
  
  try {
   
   // URL site
   url = new URL("http://free-samplecode.blogspot.com/p/java.html");
   
   //open stream
   is = url.openStream();
   
   byte[] b = new byte[1024];
   while(is.read(b) != -1){
    System.out.println(new String(b));
   }
   
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (IOException e){
   e.printStackTrace();
  }
  
 }
}


Output



How To Get URL Content In Java



Wednesday, March 9, 2016

Java Networking Example : How To Get Localhost Address And Localhost Name

Intro


To get localhost address and localhost name, you can use getLocalHost() method of InetAddress class.

Example


package com.freesamplecode.java.networking;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetLocalhostDemo {
 public static void main(String[] args){
  InetAddress address;
  try {
   address = InetAddress.getLocalHost();
   System.out.println("Localhost Address :" +address.getHostAddress());
   System.out.println("Localhost Name :" +address.getHostName());
  } catch (UnknownHostException e) {
   e.printStackTrace();
  }
  
 }
}

Output


Localhost Address :192.168.1.101
Localhost Name :mobile214

Screenshot




Java Networking Example : How To Get IP Address Of A Host

Intro


To get IP address of a host, you can use the InetAddress class. By calling getHostAddress() method, you can get an IP address of the host.

Example


package com.freesamplecode.java.networking;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetIPAddressByHost {
 public static void main(String[] args){
  try {
   InetAddress host = InetAddress.getByName("free-samplecode.blogspot.com");
   System.out.println("IP Address : "+host.getHostAddress());
  } catch (UnknownHostException e) {
   e.printStackTrace();
  }
 }
}


Ouput


IP Address : 74.125.200.132

Screenshot

How To Get IP Address In Java