0

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

}
Kelvin Lam
  • 11
  • 3
  • Duplicate - https://stackoverflow.com/questions/7210232/ruby-nethttp-following-301-redirects – user3309314 Feb 01 '18 at 03:05
  • the `http.get(url)` should be just `http.get('/asia/')`. You don't pass the url object there – Phlip Feb 01 '18 at 03:13
  • Possible duplicate of [Ruby Net::HTTP - following 301 redirects](https://stackoverflow.com/questions/7210232/ruby-nethttp-following-301-redirects) – Mark Mucha Feb 01 '18 at 03:14
  • @Phlip thanks! I tried to change it to '/asia/' and it still passed me 301 redirect :( – Kelvin Lam Feb 01 '18 at 03:20
  • and thanks for the suggestion on the duplicate - however the articles that you guys shared are more for handling 301 redirect. This issue it seems to me a more syntax related issue and even if I implement the same 301 follow code it still doesn't work... – Kelvin Lam Feb 01 '18 at 03:25
  • The standard way to call `start` is without `new`, and with the HTTPS optionally turned on: `Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|` – Phlip Feb 01 '18 at 06:47

1 Answers1

1

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

goose3228
  • 361
  • 1
  • 8
  • Btw if you all you need is page content sometimes it's simplier to use wget as system call: page_content = `wget -qO- https://example.com` – goose3228 Feb 01 '18 at 06:53