By using URLConnection object, you can query headers. Following
getter methods provided by URLConnection class, to query for header
information.
public
String getContentType()
Returns the value of the content-type header field.
public int
getContentLength()
Returns the value of the content-length header field.
Returns the content length in bytes. Returns -1 if the content length is not
known, or if the content length is greater than Integer.MAX_VALUE.
public long
getContentLengthLong()
Returns the value of the content-length header field.
Returns the content length in bytes. Returns -1 if the content length is not
known. This method is used to know the length of large files (where size
exceeds integer).
public
String getContentEncoding()
Returns the value of the content-encoding header field. It
returns null if the content encoding not known.
public long
getDate()
Returns the sending date of the resource in milliseconds.
public long
getExpiration()
Returns the expiration date of the resource.
Returns the expiration date of the resource.
import java.net.URL; import java.net.URLConnection; import java.util.Date; public class Main { public static void main(String args[]) throws Exception { String resource = "https://self-learning-java-tutorial.blogspot.com"; /* Construct URL object */ URL url = new URL(resource); /* Open URLConnection to this URL */ URLConnection conn = url.openConnection(); String contentType = conn.getContentType(); int conLengthInt = conn.getContentLength(); long contentLengthLong = conn.getContentLengthLong(); String contentEncoding = conn.getContentEncoding(); long dateInMillis = conn.getDate(); Date documentSent = new Date(dateInMillis); long expirationMillis = conn.getExpiration(); Date expireDate = new Date(expirationMillis); long lastModifiedMills = conn.getLastModified(); Date lastModifiedDate = new Date(lastModifiedMills); System.out.println("contentType = " + contentType); System.out.println("conLengthInt = " + conLengthInt); System.out.println("contentLengthLong = " + contentLengthLong); System.out.println("contentEncoding = " + contentEncoding); System.out.println("documentSent = " + documentSent); System.out.println("expireDate = " + expireDate); System.out.println("lastModifiedDate = " + lastModifiedDate); } }
Sample
Output
contentType = text/html; charset=UTF-8 conLengthInt = -1 contentLengthLong = -1 contentEncoding = null documentSent = Tue May 19 20:40:26 IST 2015 expireDate = Tue May 19 20:40:26 IST 2015 lastModifiedDate = Mon May 18 15:56:27 IST 2015
No comments:
Post a Comment