简体   繁体   中英

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']

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM