Test For JPEG Image Over HTTP

A

Alex Young

Neil said:
Hi

I would like to test for the existence of a .jpg image on a remote
server over HTTP.

I have tried 2 different methods but they are not what I need.

Method 1:

require 'open-uri'
if open('http://example.net/path/to/image.jpg')

end
require 'net/http'

# Strict existence
def url_exists?(path)
u = URI.parse(path)
h = Net::HTTP.new(u.host, u.port)
r = h.head(u.path)
return Net::HTTPOK === r
end

# Lack of non-existence
def url_found?(path)
u = URI.parse(path)
h = Net::HTTP.new(u.host, u.port)
r = h.head(u.path)
return !(Net::HTTPNotFound === r)
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,089
Members
47,687
Latest member
IngridXxj

Latest Threads

Top