You are here: Home / Topics / Program to use URLConnection class in Java

Program to use URLConnection class in Java

Filed under: Java on 2023-10-25 06:49:31

//  Program to use URLConnection class.

import java.io.*;
import java.net.*;
import java.util.Date;

class URLConnectionDemo
{
public static void main(String args[ ]) throws Exception
{
 int ch;
 URL ob = new URL("http://www.bethedeveloper.com/java/");
 URLConnection uc = u.openConnection();
 System.out.println("Content type: " + uc.getContentType());
 System.out.println("Document date: " + new Date(uc.getDate()));
 System.out.println("Last modified: " + new Date(uc.getLastModified()));
 System.out.println("Document expires: " + uc.getExpiration());
 int len = uc.getContentLength();
 System.out.println("Content length: " + len);
 if( len > 0 ) 
 {
  InputStream in = uc.getInputStream();
  while ((ch = in.read()) != -1) 
  {
   System.out.print((char) ch);
  }
  in.close();
 }
}
}

 

Output:

Content type: text/html: charset = iso-8859-1
Document date: fri Jun 24 04:42:00 IST 2016  
Last modified: wen Jun 22 03:12:03 IST 2016  
Document expires: 0
Content length: 327

About Author:
J
Java     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.