HttpURLConnection

In this chapter you will learn:

  1. How to get HttpURLConnection from a URL

Create HttpURLConnection

HttpURLConnection is a URLConnection with support for HTTP-specific features. Each HttpURLConnection instance makes a single HTTP request.

The following code gets a HttpURLConnection from URL and casts the URLConnection to HttpURLConnection.

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
// j  av 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();

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

 }
}

Next chapter...

What you will learn in the next chapter:

  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
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