Kelvin Lam
Kelvin Lam

Reputation: 11

Getting 301 redirect when using Net::HTTP.new in Ruby

I am testing the use of proxy with Net::HTTP in Ruby and found out that I keep getting 301 redirect with the code even though I type the address in the url correctly. With the same URI object the code works if I use get_response() but doesn't work if I use the below method. Not sure what went wrong there? Thanks!

Edit: This does not seem to be a 301 follow issue. It seems like something related to https but not sure how to navigate it.

proxy_addr = nil
proxy_port = nil


url = URI("https://www.bloomberg.com/asia")

Net::HTTP.new('www.bloomberg.com', nil, proxy_addr, proxy_port).start { |http|
  res = http.get(url)
  puts res.code
  puts res.message
  puts res.header['location']

}

Upvotes: 0

Views: 1000

Answers (1)

goose3228
goose3228

Reputation: 371

You need to specify use_ssl somewhere

require 'net/http'
proxy_addr = nil
proxy_port = nil

http = Net::HTTP.new('www.bloomberg.com', 443, proxy_addr, proxy_port)
http.use_ssl = true
http.start
res = http.get('/asia')
puts res.code
puts res.message
puts res.header['location']

Otherviese it connects via http, and server sends 301 redirect to https

Upvotes: 1

Related Questions