将cookie传递给Net::HTTP.start

  • 本文关键字:HTTP start Net cookie ruby
  • 更新时间 :
  • 英文 :


我现在有这个代码来找出url的重定向路径。问题是我不能传递任何饼干。

有人知道怎么做吗?

url = URI.parse("http://example.com") # Make sure you put the trailing slash on! 
found = false 
until found 
  host, port = url.host, url.port if url.host && url.port 
  req = Net::HTTP::Get.new(url.path)
  res = Net::HTTP.start(url.host, url.port) do |http| 
    http.request(req)
  end
  puts res.header['location']
  res.header['location'] ? url = URI.parse(res.header['location']) : 
found = true 
end

解决方案如下。

url = URI.parse("http://example.com")
found = false 
until found 
  host, port = url.host, url.port if url.host && url.port 
  req = Net::HTTP::Get.new(url.path, {
    "Cookie" => "sessid=123;"
  })
  res = Net::HTTP.start(url.host, url.port) do |http| 
    http.request(req)
  end
  puts res.header['location']
  res.header['location'] ? url = URI.parse(res.header['location']) : found = true 
end

相关内容

最新更新