`
hnus
  • 浏览: 27964 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ruby发送带header的http请求 (Send Custom Headers Ruby Net::HTTP)

 
阅读更多
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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics