Pin It

How to use java.net.URLConnection Class for Java Networking ?.

Program to demonstrate working of URLConnection class and the methods of the URLConnection class for Java Networking

package com.hubberspot.networking.example;

import java.net.*;
import java.io.*;
import java.util.Date;
class URLConnectionExample
{
  public static void main(String args[]) throws Exception 
  {
    int ch;
    URL url = new URL("http://www.gmail.com");
    URLConnection connection = url.openConnection();

    System.out.println("Date: " + 
    new Date(connection.getDate()));
  
    System.out.println("Content-Type: " +
    connection.getContentType());
  
    System.out.println("Expires: " + 
    connection.getExpiration());
  
    System.out.println("Last-Modified: " + 
    new Date(connection.getLastModified()));
  
    int len = connection.getContentLength();
    System.out.println("Content-Length: " + len);

    if (len > 0) {
 System.out.println("===Print Content ===");
 InputStream input = connection.getInputStream();
 int i = len;
 while (((ch = input.read()) != -1) && (--i > 0)) {
  System.out.print((char) ch);
 }
 input.close();

 } else {
  System.out.println("No Content Available");
 }
 }
}


Output of the program :


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot