Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在Ruby中使用Net::HTTP.new时获取301重定向_Ruby On Rails_Ruby_Net Http - Fatal编程技术网

Ruby on rails 在Ruby中使用Net::HTTP.new时获取301重定向

Ruby on rails 在Ruby中使用Net::HTTP.new时获取301重定向,ruby-on-rails,ruby,net-http,Ruby On Rails,Ruby,Net Http,我正在Ruby中测试代理与Net::HTTP的使用情况,发现即使我在url中正确键入了地址,我仍然会通过代码获得301重定向。对于同一个URI对象,如果使用get_response,代码可以工作,但是如果使用下面的方法,代码就不能工作。不确定哪里出了问题?谢谢 编辑:这似乎不是一个301跟踪问题。它似乎与https有关,但不确定如何导航 proxy_addr = nil proxy_port = nil url = URI("https://www.bloomberg.com/asia")

我正在Ruby中测试代理与Net::HTTP的使用情况,发现即使我在url中正确键入了地址,我仍然会通过代码获得301重定向。对于同一个URI对象,如果使用get_response,代码可以工作,但是如果使用下面的方法,代码就不能工作。不确定哪里出了问题?谢谢

编辑:这似乎不是一个301跟踪问题。它似乎与https有关,但不确定如何导航

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

}

您需要在某处指定使用ssl

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

另外,它通过http连接,服务器发送301重定向到https

Duplicate-http.geturl应该只是http.get'/asia/'。您没有传递url对象,@Phlip可能重复,谢谢!我试着把它改成“/asia/”,但它还是通过了301重定向:谢谢你的建议,但是你们分享的文章更多的是为了处理301重定向。这个问题对我来说似乎是一个与语法更相关的问题,即使我实现了相同的301跟随代码,它仍然不起作用…顺便说一句,如果您只需要页面内容,有时使用wget作为系统调用更简单:page_content=wget-qO-https://example.com