“Accept-Encoding: gzip, deflate”问题

来源:互联网 发布:stp文件打开软件 编辑:程序博客网 时间:2024/05/20 02:22

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

用ruby写的请求网页的程序,输出的文本是乱码

以下是代码,试了各种网上常见的方法,均不可行。
#encoding: UTF-8#!/usr/bin/rubyrequire '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    endendheaders = {   ##定义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.newpara['domain'] = 'access.redhat.com'para['headers'] = headerspara['uri'] = '/security/cve/CVE-2014-4877'output = sendhttp(para)puts output可以直接运行的。
提问者采纳
因为你用了 'Accept-Encoding' => 'gzip, deflate',发回来的内容是zip流,你应该去掉Accept-Encoding这一行即可。
0 0
原创粉丝点击