Ruby:URI::InvalidURIError(URI 只能是 ascii [英] Ruby: URI::InvalidURIError (URI must be ascii only

查看:54
本文介绍了Ruby:URI::InvalidURIError(URI 只能是 ascii的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

require 'uri'
uri = URI.parse 'http://dxczjjuegupb.cloudfront.net/wp-content/uploads/2017/10/Оуэн-Мэтьюс.jpg'

浏览器没有问题 http://dxczjjuegupb.cloudfront.net/wp-content/uploads/2017/10/Оуэн-Мэтьюс.jpg 所以我在问自己这个 ruby​​ 类是否有点过时了?我应该完全放弃它还是做一些错误处理......

The browsers have no problem with http://dxczjjuegupb.cloudfront.net/wp-content/uploads/2017/10/Оуэн-Мэтьюс.jpg so I'm asking myself if this ruby class is a little bit outdated? And should I completely renounce it or do some error handling…

推荐答案

通过问自己这个问题,我得到了答案:

The answer just came to me by asking myself the question:

begin
  uri = URI.parse(url)
rescue URI::InvalidURIError
  uri = URI.parse(URI.escape(url))
end

这篇关于Ruby:URI::InvalidURIError(URI 只能是 ascii的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆