Sunday, August 24, 2014
Java Networking APIs provide a set of classes to interact with the internet. The primary classes include URL, URLConnection and HttpURLConnection. This tutorial provides examples to construct URLs and to access internet resources. 

Read properties of URL

URL represents the Uniform Resource Locator a resource on the world wide web. 
URL url1 = new URL("http", "www.sourcetricks.com", 
              8080, "p/java-tutorials.html");
System.out.println(url1.toString());

This program produces the following output printing the URL as string.
http://www.sourcetricks.com:8080p/java-tutorials.html

Class URL provides functions to retrieve the properties of an URL object including protocol, host, port, path etc. Following program demonstrates reading the properties of an URL.

public static void main(String[] args) throws Exception {
  URL url = new URL("http://www.sourcetricks.com/p/java-tutorials.html");
  System.out.println(url.getHost());
  System.out.println(url.getPort());
  System.out.println(url.getProtocol());
  System.out.println(url.getPath());
}

This program produces the following output printing the URL properties.
www.sourcetricks.com
-1
http
/p/java-tutorials.html

Using URLConnection

URLConnection is used for accessing remote resources. We can examine the attributes of a remote resource specified as an URL and also get the contents locally.

public static void main(String[] args) throws Exception {
  URL url = new URL("http://www.sourcetricks.com/p/java-tutorials.html");
  URLConnection connection = url.openConnection();
  BufferedReader reader = new BufferedReader
     (new InputStreamReader(connection.getInputStream()));
  String line = null;
  while ( ( line = reader.readLine() ) != null ) {
    System.out.println(line);
  }
  reader.close();  
}

Using HttpURLConnection

HttpURLConnection is a sub-class of URLConnection class and includes support for handling HTTP connections. HttpURLConnection class has additional methods like HTTP request method, HTTP response code, HTTP redirect handling etc.

In the example below we make a HttpURLConnection to get a page contents. Please note that additional methods specific to HTTP can be used on the connection object.

public static void main(String[] args) throws Exception {
  URL url = new URL("http://www.sourcetricks.com/p/java-tutorials.html");
  HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
  System.out.println(httpConnection.getRequestMethod());
  System.out.println(httpConnection.getResponseCode());
  System.out.println(httpConnection.getResponseMessage());
  BufferedReader reader1 = new BufferedReader
     (new InputStreamReader(httpConnection.getInputStream()));
  String line1 = null;
  while ( ( line1 = reader1.readLine() ) != null ) {
    System.out.println(line1);
  }
  reader1.close();
  httpConnection.disconnect();
}

This program produces the following output displaying the HTTP request type (GET), HTTP response code (200) and HTTP response message (OK). Finally it prints the page contents.

GET
200
OK
Page contents ...

Continue to read other java tutorials from here.

0 comments :

Post a Comment

Contact Form

Name

Email *

Message *

Back to Top