Demonstrate URLConnection. : URLConnection « Network Protocol « Java

Demonstrate URLConnection.

import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;

public class MainClass {
  public static void main(String args[]) throws Exception {
    int c;
    URL hp = new URL("");
    URLConnection hpCon = hp.openConnection();

    long d = hpCon.getDate();
    if (d == 0)
      System.out.println("No date information.");
      System.out.println("Date: " + new Date(d));

    System.out.println("Content-Type: " + hpCon.getContentType());

    d = hpCon.getExpiration();
    if (d == 0)
      System.out.println("No expiration information.");
      System.out.println("Expires: " + new Date(d));

    d = hpCon.getLastModified();
    if (d == 0)
      System.out.println("No last-modified information.");
      System.out.println("Last-Modified: " + new Date(d));

    int len = hpCon.getContentLength();
    if (len == -1)
      System.out.println("Content length unavailable.");
      System.out.println("Content-Length: " + len);

    if (len != 0) {
      InputStream input = hpCon.getInputStream();
      int i = len;
      while (((c = != -1)) { // && (--i > 0)) {
        System.out.print((char) c);

    } else {
      System.out.println("No content available.");



Related examples in the same category

1.Call a servlet from a Java command line application
2.A CGI POST Example
3.Http authentication header
5.File size from URL
6.Sending a POST Request Using a URL
7.Check if a file was modified on the server
8.Getting Text from a URL
9.Getting an Image from a URL
10.Sending a POST Request with Parameters From a Java Class
11.Downloading a web page using URL and URLConnection classes
12.Get response header from HTTP request
13.Read / download webpage content
14.Read a GIF or CLASS from an URL save it locally
15.Zip URLConnection
16.Zip URLStream Handler
17.Http Parser
18.Http Constants
19.Get URLConnection Expiration
20.Locating files by path or URL
21.Download from URL
22.This program demonstrates how to use the URLConnection class for a POST request.
23.Connects to an URL and displays the response header data and the first 10 lines of the requested data.Connects to an URL and displays the response header data and the first 10 lines of the requested data.
24.Load content from URL to string