我安装了zlib的ruby-1.9.3-p327.localhost:80
是nginx简单的测试页面.
require "net/http"
=> true
Net::HTTP::HAVE_ZLIB
=> true
res = Net::HTTP.start("localhost", "80") do |http|
req = Net::HTTP::Get.new "/"
req["accept-encoding"] = "gzip"
http.request req
end
=> #<Net::HTTPOK 200 OK readbody=true>
res.get_fields "content-encoding"
=> ["gzip"]
res.body
=> "\x1F\x8B\b\x00\x00\x00\x00\x00\x00\x03\xEC\xBDi..."
Run Code Online (Sandbox Code Playgroud)
身体没有被解码.为什么?
对于那些在ruby 1.9上遇到代码问题并且无法升级到ruby 2.0的人来说,只需将该代码包含在项目中即可.
module HTTPResponseDecodeContentOverride
def initialize(h,c,m)
super(h,c,m)
@decode_content = true
end
def body
res = super
if self['content-length']
self['content-length']= res.bytesize
end
res
end
end
module Net
class HTTPResponse
prepend HTTPResponseDecodeContentOverride
end
end
Run Code Online (Sandbox Code Playgroud)
如果你使用http.get
它应该自动解码它,但看起来request
可能不适合你.
显然有代码在这里解压缩gzip请求,但仅适用于该get
方法:https:
//github.com/ruby/ruby/blob/v1_9_3_327/lib/net/http.rb#L1031
归档时间: |
|
查看次数: |
3664 次 |
最近记录: |