MATLAB: How to know whether it is an image or a website with a given url

url

Hi
I have a few urls, each of which begin with 'http://' and end with '.jpg'.
Some lead to images and others lead to websites (e.g. the above url).
Is there a way to know whether each url leads to an image or a website? If it leads to an image, save it; if it leads to a website, do nothing.
Can anyone help?

Best Answer

The way matlab (or any other web client) determine the type content of a uri is by looking at the Content-Type property returned in the header of the get (or post) request that matlab (or any other web client) send when you request that page. All of this is hidden from you by webread and websave.
Since R2016b, matlab gives you functions to get down to the nitty gritty of the http protocol. The following should work:
queryurl = 'http://anguerde.com/pics/main/2/218886-active.jpg'; %replace as required
uri = matlab.net.URI(queryuri);
request = matlab.net.http.RequestMessage;
response = request.send(uri);
contenttype = response.Header.getFields('Content-Type');
If the uri points to an image, then Content-Type should contain 'image/something', so you could do
isimage = contains(contenttype.Value, 'image/')
Note that if you need to pass query parameters, or log into the web page, or some other things, there's a lot more work that needs to be performed before the above would work.
Related Question