麻烦用Ruby的’open-uri’打开utf-8 URI
我正在尝试使用ruby和open-uri从谷歌地图Web服务API获取丹麦的位置地址。
试图让Ærø,丹麦 : http : //maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false®ion=dk在Chrome中运行时不支持open-uri:
require 'rubygems' require "open-uri" require 'json' uri = "http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false®ion=dk" response = open(uri) array = JSON.parse(response) pp array
在这里它产生
/usr/lib/ruby/1.8/uri/common.rb:436:in `split': bad URI(is not URI?): http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false®ion=dk (URI::InvalidURIError)
另一种方法是逃避角色:
uri = "http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false®ion=dk" uri_escaped = URI.escape(uri) response = open(uri_escaped) array = JSON.parse(response.read) pp array
但这会产生一个逃脱的结果(这不是追求:-)
任何人都知道什么可以解决这个问题(获得未转发的反馈或发送utf-8请求)?
这里的Ruby版本是1.8.7
弄清楚了:
只需添加
require 'string19'
到第二个例子的顶部,它的工作原理