On 7/19/06, Just Another Victim of the Ambient Morality
<ihatespam / rogers.com> wrote:
>
> "Eric Hodel" <drbrain / segment7.net> wrote in message
> news:087CE005-B271-4B73-9C98-7DF62B1AC652 / segment7.net...
> >
> > $ ruby -rnet/http
> > Net::HTTP.start 'en.wikipedia.org' do |http| res = http.get '/'; p  res;
> > end
> > #<Net::HTTPMovedPermanently 301 Moved Permanently readbody=true>
> >
> > Matches browser behavior.
> >
> >> Hell, "/" at "music.com" has _never_ worked and there doesn't  appear to
> >> be any kind of redirect or anything.
> >
> > $ ruby -rnet/http
> > Net::HTTP.start 'music.com' do |http| res = http.get '/'; p res; end
> > #<Net::HTTPMovedPermanently 301 Moved Permanently readbody=true>
> >
> > Redirects to www.music.com in the browser, Net::HTTP matches browser
> > behavior.
>
>     Okay, so the site doesn't exist where I think it does and I get
> redirected... in my browser.  Net::HTTP doesn't get redirected, it simply
> fails.  _This_ behaviour doesn't match my browser.  Is there any way I can
> get redirected or find out where it wants to redirect me and go there,
> myself?

You might want to check out the API docs for Net::HTTP.

Copy/pasted from
http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html:


    require 'net/http'
    require 'uri'

    def fetch(uri_str, limit = 10)
      # You should choose better exception.
      raise ArgumentError, 'HTTP redirect too deep' if limit == 0

      response = Net::HTTP.get_response(URI.parse(uri_str))
      case response
      when Net::HTTPSuccess     then response
      when Net::HTTPRedirection then fetch(response['location'], limit - 1)
      else
        response.error!
      end
    end

    print fetch('http://www.ruby-lang.org')