HttpURLConnection
In this chapter you will learn:
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:
- Get the document expiration date
- Get the document Last-modified date
- Show the content type
- Show the content length
- Request method
- Get response code and message
- Check if a page exists
Home » Java Tutorial » URL URI