ruby-on-rails – Ruby / Rails – 错误的URI
发布时间:2020-12-17 01:53:08 所属栏目:百科 来源:网络整理
导读:当URI在浏览器中正常工作时,不确定为什么我收到以下错误: http://oracleofbacon.org/cgi-bin/xml?a=Kevin Baconb=Tom Cruiseu=1p=google-apps 这是我的代码: def kb(to) uri = "http://oracleofbacon.org/cgi-bin/xml?a=Kevin Baconb=#{to.strip}u=1p=goog
当URI在浏览器中正常工作时,不确定为什么我收到以下错误:
http://oracleofbacon.org/cgi-bin/xml?a=Kevin Bacon&b=Tom Cruise&u=1&p=google-apps 这是我的代码: def kb(to) uri = "http://oracleofbacon.org/cgi-bin/xml?a=Kevin Bacon&b=#{to.strip}&u=1&p=google-apps" doc = Nokogiri::XML(open(uri)) # throws error on this line return parse(doc) end 我收到以下错误: in `split': bad URI(is not URI?): http://oracleofbacon.org/cgi-bin/xml?a=Kevin Bacon&b=Tom Cruise&u=1&p=google-apps (URI::InvalidURIError)` 我按以下方式执行方法: kb("Tom Cruise") 解决方法
这是因为浏览器在路径上是友好的,就像小狗一样,并且会不遗余力地渲染页面或解析URL.应用程序不会这样做,因为你必须告诉它如何友好.
您的网址无效,因为它有嵌入空格.用以下内容替换空格: irb -f irb(main):001:0> require 'open-uri' => true irb(main):002:0> open('http://oracleofbacon.org/cgi-bin/xml?a=Kevin%20Bacon&b=Tom%20Cruise&u=1&p=google-apps').read => "<?xml version="1.0" standalone="no"?>n<link><actor>Tom Cruise</actor><movie>A Few Good Men (1992)</movie><actor>Kevin Bacon</actor></link>" 转义需要转义的字符很容易: irb -f irb(main):001:0> require 'uri' => true irb(main):002:0> URI.escape('http://oracleofbacon.org/cgi-bin/xml?a=Kevin Bacon&b=Tom Cruise&u=1&p=google-apps') => "http://oracleofbacon.org/cgi-bin/xml?a=Kevin%20Bacon&b=Tom%20Cruise&u=1&p=google-apps" (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |