Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Não trata HTTPRedirect #17

Open
chulao opened this issue Jun 22, 2016 · 1 comment
Open

Não trata HTTPRedirect #17

chulao opened this issue Jun 22, 2016 · 1 comment

Comments

@chulao
Copy link

chulao commented Jun 22, 2016

Hoje não está tratando a questão do Redirect.

Segue código funcional:

require 'rubygems'
require 'nokogiri'
require 'uri'
require 'net/https'
require 'csv'

def fetch(uri_str, username, password, limit = 10)
  # You should choose a better exception.
  raise ArgumentError, 'too many HTTP redirects' if limit == 0

  uri = URI(uri_str)
  req = Net::HTTP::Get.new(uri.request_uri)
  req.basic_auth username, password

  response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => (uri.scheme == 'https')) {|http|
    http.request(req)
  }

  case response
  when Net::HTTPSuccess then
    response
  when Net::HTTPRedirection then
    location = response['location']
    warn "Redirected to #{location}"
    fetch(location, username, password, limit - 1)
  else
    response.value
  end
end

def loadServers(body)
    doc = Nokogiri::HTML(body)
    csv = CSV.open("/tmp/servers.csv", 'w',{:col_sep => ";", :quote_char => '\'', :force_quotes => false})

    doc.xpath('//table/tbody/tr').each do |row|
        tarray = []
        row.xpath('td').each do |cell| cell
                tarray << cell.text
        end
        csv << tarray
    end
end

username = "#{ARGV[0]}"
password = "#{ARGV[1]}"
url = "#{ARGV[2]}"

res = fetch(url, username, password)

code = res.code

if code == "200" then
   loadServers(res.body)
else
   puts "Wiki is not available. Return code '#{code}'"
end
@brfso
Copy link
Owner

brfso commented Sep 23, 2016

How can i reproduce that ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants