打开URI - 无效的URI错误,编码/转义不会影响 [英] Open URI - Invalid URI Error, encoding/escaping not affecting

查看:1929
本文介绍了打开URI - 无效的URI错误,编码/转义不会影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



代码:


$

我建立一个YahooFinance Api,并尝试使用打开的URI时, b $ b

  uri =(http://ichart.finance.yahoo.com/table.csv?s=#{URI.escape(code)}&a= #{start_month}& b =#{start_day}& c =#{start_year}& d =#{end_month}& e =#{end_day}& f =#{end_year}& g = d& ignore = .csv)
puts#{uri}
conn = open(uri)


b $ b

错误:

 `split':坏URI(不是URI?):http:// ichart .finance.yahoo.com / table.csv?s =%255EIXIC& a = 00& b = 1& c = 1994& d = 09& e = 14& f = 2014& g = d& ignore = .csv} URI :: InvalidURIError)



我尝试了 URI.unescape / code>,其输出代码 ^ IXIC ,并保留任何 URI 方法输出和代码将通过%5EIXIC



在堆栈溢出后,我试过这两种方法无效:



uri = URI.parse(URI.encode(url.strip))



safeurl = URI.encode .strip)



即使查看了另一个ruby yahoo-finance gem的代码,这里,我似乎找不到解决方案。任何帮助是非常感激。感谢



编辑:我可以使用 open(uri)当我手动输入url单引号。双引号(用于插入ruby对象)在这里起作用?

解决方案

不要尝试将变量注入网址。如果它们包含需要根据规范编码的字符,则它们不会通过插值。相反,利用正确的工具,如Ruby的URI类或Addressable :: URI gem。



请参阅如何发布包含大括号和冒号的网址,以了解如何使用经过良好测试的轮盘。



你的情况,这样的东西会工作:

  require'uri'

code ='qwer3456 * & ^%'
start_month = 1
start_day = 1
start_year = 2014
end_month = 12
end_day = 31
end_year = 2015

uri = URI.parse(http://ichart.finance.yahoo.com/table.csv)
uri.query = URI.encode_www_form(
{
'g'=>'d',
'ignore'=>'.csv',
's'=> code,
'a'=> start_month,
'b'=> start_day,
'c'=> start_year,
'd'=> end_month,
'e'=> end_day,
'f'=> end_year
}

uri.to_s#=> http://ichart.finance.yahoo.com/table.csv?g=d&ignore=.csv&s=qwer3456*%26%5E%25&a=1&b=1&c=2014&d = 12& e = 31& f = 2015


I'm building out a YahooFinance Api and keep hitting a brick wall when trying to use open URI.

Code:

uri = ("http://ichart.finance.yahoo.com/table.csv?s=#{URI.escape(code)}&a=#{start_month}&b=#{start_day}&c=#{start_year}&d=#{end_month}&e=#{end_day}&f=#{end_year}&g=d&ignore=.csv")
    puts "#{uri}"
    conn = open(uri)

Error:

`split': bad URI(is not URI?): http://ichart.finance.yahoo.com/table.csv?s=%255EIXIC&a=00&b=1&c=1994&d=09&e=14&f=2014&g=d&ignore=.csv} (URI::InvalidURIError)

I have tried URI.unescape(code) which outputs code as ^IXIC, as well as leaving any URI methods out and code will come through as %5EIXIC.

After reading around on stack overflow, I've tried both of these methods to no avail:

uri = URI.parse(URI.encode(url.strip))

safeurl = URI.encode(url.strip)

Even after looking through the code for another ruby yahoo-finance gem, here, I can't seem to find a solution. Any help is greatly appreciated. Thanks

EDIT: I am able to use open(uri) when I manually enter in the url in single quotes. Do double quotes, (used for inserting ruby objects), play a role here?

解决方案

Don't try to inject variables into URLs. If they contain characters that need to be encoded per the spec, they won't be by interpolation. Instead, take advantage of the right tools for the job, like Ruby's URI class or the Addressable::URI gem.

See "How to post a URL containting curly braces and colons" for how to do this using well tested wheels.

In your situation, something like this will work:

require 'uri'

code = 'qwer3456*&^%'
start_month = 1
start_day = 1
start_year = 2014
end_month = 12
end_day = 31
end_year = 2015

uri = URI.parse("http://ichart.finance.yahoo.com/table.csv")
uri.query = URI.encode_www_form(
  {
    'g' => 'd',
    'ignore' => '.csv',
    's' => code,
    'a' => start_month,
    'b' => start_day,
    'c' => start_year,
    'd' => end_month,
    'e' => end_day,
    'f' => end_year
  }
)
uri.to_s # => "http://ichart.finance.yahoo.com/table.csv?g=d&ignore=.csv&s=qwer3456*%26%5E%25&a=1&b=1&c=2014&d=12&e=31&f=2015"

这篇关于打开URI - 无效的URI错误,编码/转义不会影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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