HttpURLConnection Properties

In this chapter you will learn:

  1. Get the document expiration date
  2. Get the document Last-modified date
  3. Show the content type
  4. Show the content length
  5. Request method
  6. Get response code and message
  7. Check if a page exists

Get the document expiration date

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
/* j  av  a  2 s . c o m*/
public class Main{
  public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.java2s.com");
    HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

    long date = httpCon.getExpiration();
    if (date == 0)
      System.out.println("No expiration information.");
    else
      System.out.println("Expires: " + new Date(date));

 }
}

The code above generates the following result.

Get the document Last-modified date

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
//j a  v a2 s  . c  o m
public class Main{
  public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.java2s.com");
    HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

    long date = httpCon.getLastModified();
    if (date == 0)
      System.out.println("No last-modified information.");
    else
      System.out.println("Last-Modified: " + new Date(date));

 }
}

The code above generates the following result.

Show the content type

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
//j  a  v  a 2 s  . c  om
public class Main{
  public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.google.com");
    HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();


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

 }
}

The code above generates the following result.

Show the content length

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
//from  j  av a 2s .com
public class Main{
  public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.google.com");
    HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();


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

The code above generates the following result.

Request method

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
//from   j  av a  2  s  . com
public class Main{
  public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.java2s.com");
    HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

    System.out.println("Request method is " + httpCon.getRequestMethod());

 }
}

Get response code and message

import java.net.HttpURLConnection;
import java.net.URL;
/*from  j a va2s  .  c  om*/
public class Main{
  public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.java2s.com");
    HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

    System.out.println("Response Message is " + httpCon.getResponseMessage());

    System.out.println("Response code is " + httpCon.getResponseCode());
 }
}

The code above generates the following result.

Check if a page exists

The following code checks the existence of a web page by looking at its response code.

import java.net.HttpURLConnection;
import java.net.URL;
//j  av  a  2s . c  om
public class Main {
  public static void main(String[] argv) throws Exception {
    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection con = (HttpURLConnection) 
           new URL("http://www.google.coom").openConnection();
    con.setRequestMethod("HEAD");
    System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_OK);
  }
}

Next chapter...

What you will learn in the next chapter:

  1. Identify ourself to a proxy
Home » Java Tutorial » URL URI
URL
URL Creation
URL for jar file
URL Components
Convert file path to URL
URL Relative
URL Protocol
Read from URL
Compare URL
URLConnection
HTTP Header
URLConnection Post
Cookie
URLConnection Read
HttpURLConnection
HttpURLConnection Properties
HttpURLConnection proxy
HttpURLConnection Authenticator
HTTPS
JarURLConnection
Encode a URL
Decode a URL
URI
URI Normalization
URI Resolution
URI Relativization
Convert URI to URL
IP Address
IP Ping
NetworkInterface