Подтвердить что ты не робот

Ruby - net/http - после перенаправления

У меня есть URL-адрес, и я использую HTTP GET для передачи запроса на страницу. Что происходит с самым последним ароматом (в net/http), так это то, что script не выходит за рамки ответа 302. Я пробовал несколько разных решений; HTTPClient, net/http, Rest-Client, Patron...

Мне нужно продолжить переход к последней странице, чтобы проверить тег атрибута на страницах html. Перенаправление происходит из-за того, что мобильный пользовательский агент нажимает страницу, которая перенаправляется на мобильный вид, следовательно, мобильный пользовательский агент в заголовке. Вот мой код, как сегодня:

require 'uri'
require 'net/http'

class Check_Get_Page

    def more_http
        url = URI.parse('my_url')
        req, data = Net::HTTP::Get.new(url.path, {
        'User-Agent' => 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5'
        })
        res = Net::HTTP.start(url.host, url.port) {|http|
        http.request(req)
            }
        cookie = res.response['set-cookie']
        puts 'Body = ' + res.body
        puts 'Message = ' + res.message
        puts 'Code = ' + res.code
        puts "Cookie \n" + cookie
    end

end

m = Check_Get_Page.new
m.more_http

Любые предложения будут очень благодарны!

4b9b3361

Ответ 1

Чтобы выполнить перенаправления, вы можете сделать что-то подобное (взято из ruby-doc)

Следующее перенаправление

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

  url = URI.parse(uri_str)
  req = Net::HTTP::Get.new(url.path, { 'User-Agent' => 'Mozilla/5.0 (etc...)' })
  response = Net::HTTP.start(url.host, url.port) { |http| http.request(req) }
  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/')

Ответ 2

Я написал для этого другой класс, основанный на приведенных здесь примерах, спасибо вам всем. Я добавил куки, параметры и исключения и, наконец, получил то, что мне нужно: https://gist.github.com/sekrett/7dd4177d6c87cf8265cd

require 'uri'
require 'net/http'
require 'openssl'

class UrlResolver
  def self.resolve(uri_str, agent = 'curl/7.43.0', max_attempts = 10, timeout = 10)
    attempts = 0
    cookie = nil

    until attempts >= max_attempts
      attempts += 1

      url = URI.parse(uri_str)
      http = Net::HTTP.new(url.host, url.port)
      http.open_timeout = timeout
      http.read_timeout = timeout
      path = url.path
      path = '/' if path == ''
      path += '?' + url.query unless url.query.nil?

      params = { 'User-Agent' => agent, 'Accept' => '*/*' }
      params['Cookie'] = cookie unless cookie.nil?
      request = Net::HTTP::Get.new(path, params)

      if url.instance_of?(URI::HTTPS)
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      end
      response = http.request(request)

      case response
        when Net::HTTPSuccess then
          break
        when Net::HTTPRedirection then
          location = response['Location']
          cookie = response['Set-Cookie']
          new_uri = URI.parse(location)
          uri_str = if new_uri.relative?
                      url + location
                    else
                      new_uri.to_s
                    end
        else
          raise 'Unexpected response: ' + response.inspect
      end

    end
    raise 'Too many http redirects' if attempts == max_attempts

    uri_str
    # response.body
  end
end

puts UrlResolver.resolve('http://www.ruby-lang.org')

Ответ 3

Ссылка, которая работала для меня, была здесь: http://shadow-file.blogspot.co.uk/2009/03/handling-http-redirection-in-ruby.html

По сравнению с большинством примеров (включая принятый ответ здесь), он более надежный, поскольку он обрабатывает URL-адреса, которые являются просто доменом (http://example.com - нужно добавить /), обрабатывает SSL специально, а также относительные URL-адреса.

Конечно, в большинстве случаев вам будет лучше использовать библиотеку, такую ​​как RESTClient, но иногда требуется детализация низкого уровня.

Ответ 4

Возможно, вы можете использовать curb-fu gem здесь https://github.com/gdi/curb-fu, единственное, что есть дополнительный код, чтобы заставить его следовать переадресации. Раньше я использовал следующее. Надеюсь, что это поможет.

require 'rubygems'
require 'curb-fu'

module CurbFu
  class Request
    module Base
      def new_meth(url_params, query_params = {})
        curb = old_meth url_params, query_params
        curb.follow_location = true
        curb
      end

      alias :old_meth :build
      alias :build :new_meth
    end
  end
end

#this should follow the redirect because we instruct
#Curb.follow_location = true
print CurbFu.get('http://<your path>/').body