Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

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 :


 
© 2021 Learn Java by Examples Template by Hubberspot