Ruby 请求 Zip 压缩的 HTTP 页面

2015-11-10 0 803
Ruby 请求 Zip 压缩的 HTTP 页面
require 'net/http'
require 'uri'

module Net
  class HTTP
    def HTTP.get_with_headers(uri, headers=nil)
      uri = URI.parse(uri) if uri.respond_to? :to_str
      start(uri.host, uri.port) do |http|
        path_query = uri.path + (uri.query ? ('?' + uri.query) : '')
        return http.get(path_query, headers)
      end
    end
  end
end

gzipped = Net::HTTP.get_with_headers('http://www.cnn.com/',
                                     {'Accept-Encoding' => 'gzip'})
puts gzipped['Content-Encoding']
puts gzipped.body.size

遇见资源网 ruby Ruby 请求 Zip 压缩的 HTTP 页面 http://www.ox520.com/16536.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务