HttpURLConnection Properties
In this chapter you will learn:
- 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
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:
Home » Java Tutorial » URL URI