【发布时间】:2013-12-22 07:07:31
【问题描述】:
我正在尝试使用Net::HTTP 向特定网站发出 POST 请求,并在返回响应后开始下载文件。
我首先用 wget 进行了测试,它运行良好。我还尝试了 httpi 和 unirest gems,它运行良好。但是,因为Net::HTTP 是低级API,我需要更多的控制(例如,跟踪下载进度),所以我会坚持下去。
问题是,下载只是不会开始。所以这里是代码:
require 'net/http'
require 'tempfile'
uri = URI.parse("http://mywebsite.com")
req = Net::HTTP::Post.new(uri.path)
req.set_form_data({'filed' => 'data'})
response = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req) do |response|
temp_file = Tempfile.new("new_file")
response.read_body do |chunk|
temp_file << chunk
end
end
}
我想问题一定是在回应,我得到的不是Net::HTTPOK
但是HTTPSeeOther 这里有更多关于它的信息
#<Net::HTTPSeeOther:0x91f2670>
{"server"=>["nginx"], "date"=>["Sat, 21 Dec 2013 22:32:14 GMT"],
"content-type"=>["text/html"], "transfer-encoding"=>["chunked"],
"connection"=>["keep-alive"], "pragma"=>["no-cache"],
"cache-control"=>["no-cache, must-revalidate"],
"expires"=>["Sat, 26 Jul 1997 05:00:00 GMT"],
"location"=>["http://mywebsite.com/path/to/file.rar"]}
我提到的其他 2 个 gem 知道如何自动开始下载,但我不知道如何处理 HTTPSeeOther。
【问题讨论】: