ruby-on-rails – RubyRails – 错误的URI

ruby-on-rails – RubyRails – 错误的URI,第1张

概述当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 当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 -firb(main):001:0> require 'open-uri'=> trueirb(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 -firb(main):001:0> require 'uri'=> trueirb(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"
总结

以上是内存溢出为你收集整理的ruby-on-rails – Ruby / Rails – 错误的URI全部内容,希望文章能够帮你解决ruby-on-rails – Ruby / Rails – 错误的URI所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1260128.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存