uri = URI('http://example.com/cached_response')
file = File.stat 'cached_response'
req = Net::HTTP::Get.new(uri)
req['If-Modified-Since'] = file.mtime.rfc2822
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
open 'cached_response', 'w' do |io|
io.write res.body
end if res.is_a?(Net::HTTPSuccess)require "net/http"require "uri"url = URI.parse("http://www.whatismyip.com/automation/n09230945.asp")req = Net::HTTP::Get.new(url.path)req.add_field("X-Forwarded-For", "0.0.0.0")res = Net::HTTP.new(url.host, url.port).start do |http| http.request(req)endputs res.body
http://stackoverflow.com/questions/587559/how-to-make-an-http-get-with-modified-headers
require 'uri'
require 'net/http'
size = 1000 #the last offset (for the range header)
uri = URI("http://localhost:80/index.html")
http = Net::HTTP.new(uri.host, uri.port)
headers = {
'Range' => "bytes=#{size}-"
}
path = uri.path.empty? ? "/" : uri.path
#test to ensure that the request will be valid - first get the head
code = http.head(path, headers).code.to_i
if (code >= 200 && code < 300) then
#the data is available...
http.get(uri.path, headers) do |chunk|
#provided the data is good, print it...
print chunk unless chunk =~ />416.+Range/
end
end
http://ruby-doc.org/stdlib-2.1.1/libdoc/net/http/rdoc/Net/HTTP.html
http://stackoverflow.com/questions/587559/how-to-make-an-http-get-with-modified-headers
http://ruby-doc.org/stdlib-2.1.1/libdoc/net/http/rdoc/Net/HTTP.html
本文探讨了如何利用HTTP头部信息实现对已缓存资源的高效请求与利用,包括使用If-Modified-Since和Range头部进行条件请求,以及在特定场景下如何获取更新的数据。
&spm=1001.2101.3001.5002&articleId=84710551&d=1&t=3&u=d263a4ba82fc4767b60c6af32b3c0635)
472

被折叠的 条评论
为什么被折叠?



