I am using the Selenium 2 Java API to interact with web pages. My question is: How can i detect the content type of link destinations?
Basically, this is the background: Before clicking a link, i want to be sure that the response is an HTML file. If not, i need to handle it in another way. So, let’s say there is a download link for a PDF file. The application should directly read the contents of that URL instead of opening it in the browser.
The goal is to have an application which automatically knows wheather the current location is an HTML, PDF, XML or whatever to use appropriate parsers to extract useful information out of the documents.
Update
Added bounty: Will reward it to the best solution which allows me to get the content type of a given URL.
As Jochen suggests, the way to get the Content-type without also downloading the content is
HTTP HEAD, and the selenium webdrivers does not seem to offer functionality like that. You’ll have to find another library to help you with fetching the content type of an url.A Java library that can do this is Apache HttpComponents, especially HttpClient.
(The following code is untested)
The project publishes JavaDoc for HttpClient, the documentation for the HttpClient interface contains a nice example.