How do you determine if a file is html from the URL?
html, java, url
Solution
Just from the URL you cannot, think of the following urls:
- http://host1/index.html
- http://host2/index.php
- http://host3/index.asp
- http://host4/index.jsp
- http://host5/index.aspx
- Or the the url of this question - How do you determine if a file is html from the URL?
All of them return HTML content. The only sure way is to ask the server for the resource, and check the Content-TYpe header. It is better to use to send an HEAD request to the server, instead of GET or POST - it will give you just the headers and without the content.
URL url = ...
HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
urlc.setAllowUserInteraction( false );
urlc.setDoInput( true );
urlc.setDoOutput( false );
urlc.setUseCaches( true );
urlc.setRequestMethod("HEAD");
urlc.connect();
String mime = urlc.getContentType();
if(mime.equals("text/html") {
// do your stuff
}
Problem
Given a URL, how can you tell if the referenced file is and html file? Obviously, its an html file if it ends in .html or /, but then there are .jsp files, too, so I'm wondering what other extensions may be out there for html. Alternatively, if this information can be easily gained from a URL object in Java, that would be sufficient for my purposes.