1. 程式人生 > >“Accept-Encoding: gzip, deflate”問題

“Accept-Encoding: gzip, deflate”問題

轉載至http://zhidao.baidu.com/link?url=fG_OjaFDrklRpdeRpH_tlALrvNibI_IQnfMElOtdzNQ5MiLqM_4L039LCYXfPe-b7-5eAqGRbw67LEVDEVSy-KtqmQeDGasYY3qUQXOJ1jy

用ruby寫的請求網頁的程式,輸出的文字是亂碼

以下是程式碼,試了各種網上常見的方法,均不可行。
#encoding: UTF-8
#!/usr/bin/ruby

require 'net/http'
require 'nokogiri'
require 'uri' 
require 'utf8-cleaner'
require 'iconv'

def sendhttp(hash)
	    http = Net::HTTP.new(hash['domain'],443)  
	    http.use_ssl = true  
	    resp = http.get(hash['uri'],hash['headers'])
	    if resp.code == '200'
		        return resp.body
	    else
		        return false
	    end
end

headers = {   ##定義http請求頭資訊  
  'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:18.0) Gecko/20100101 Firefox/18.0',
  'Content-Type' => 'application/x-www-form-urlencoded',  
  'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'Accept-Language' => 'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3',
  'Accept-Encoding' => 'gzip, deflate',
  'Connection' => 'keep-alive',
}  

para 	= Hash.new
para['domain'] 	= 'access.redhat.com'
para['headers'] = headers
para['uri'] 	= '/security/cve/CVE-2014-4877'

output 	= sendhttp(para)
puts output
可以直接執行的。
提問者採納
因為你用了 'Accept-Encoding' => 'gzip, deflate',發回來的內容是zip流,你應該去掉Accept-Encoding這一行即可。