how to judge web exists or not with open-uri?

P

Pen Ttt

would you mind to tell me how to fix it, to make the following program
to run?
require 'rubygems'
require 'open-uri'
url="www.123.com"
open(url){|file| if url don't exists then puts "url don't exists"
else puts "url exists"}
thiks
 
R

Robert Klemme

2010/7/20 Pen Ttt said:
would you mind to tell me how to fix it, to make the following program
to run?
require 'rubygems'
require 'open-uri'
url="www.123.com"
open(url){|file| if url don't exists then puts "url don't exists"
else puts "url exists"}

You better use Net::HTTP for this
http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html

url = URI.parse('www.123.com')
status = Net::HTTP.start(url.host, url.port) {|http|
http.head(url.path)
}

puts( /^2/ =~ status.code ? "ok" : "nok" )

Kind regards

robert
 
P

Pen Ttt

irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'open-uri'
=> true
irb(main):003:0> url = URI.parse('www.123.com')
=> #<URI::Generic:0xb764ff84 URL:www.123.com>
irb(main):004:0> status = Net::HTTP.start(url.host, url.port) {|http|
irb(main):005:1* http.head(url.path)
irb(main):006:1> }
NameError: uninitialized constant Net
from (irb):4
from :0
irb(main):007:0> puts( /^2/ =~ status.code ? "ok" : "nok" )
NoMethodError: undefined method `code' for nil:NilClass
from (irb):7
from :0
??
 
J

Jean-Julien Fleck

NameError: uninitialized constant Net

require 'net/http'

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
 

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
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top