2011-02-15 37 views
3

我试图从URL下载HTML内容而没有成功。使用Ruby转义和下载URL

这里是网址:

http://example.com/some_string[value] 

当使用RESTClient实现我得到这个错误:

URI::InvalidURIError: bad URI(is not URI?) 

我从Ruby on Rails的IRC一些帮助。这个想法是为了逃避URL的结束。

$ "http://example.com/" + CGI::escape("some_string[value]") 
=> "http://example.com/some_string%5Bvalue%5D" 

生成的URL不工作,我得到一个404 它可以在浏览器虽然。

任何人都知道如何让它工作?

回答

2

按照URI RFC

Other characters are excluded because gateways and other transport agents are known to sometimes modify such characters, or they are used as delimiters.

unwise = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"

Data corresponding to excluded characters must be escaped in order to be properly represented within a URI.

信任浏览器的反应或处理能力的链接是有风险的。他们尽其所能地返回页面,而不是强制执行标准,因此无论页面或URL是否被正确定义,它们都不是权威来源。

RestClient的响应可能基于URI,当我测试使用URI解析URL时,它返回相同的错误。

我从未见过使用未编码“[”和“]”字符的URL。

+0

谢谢,工作完美。我用这个来逃避网址。 `url.gsub(/ \ {| \} | \ || \\ | \^| \ [| \] | \`| \ s + /){| m | CGI :: escape(m)}`我躲过了正则表达式中的每个字符,以防万一:) – Oleander 2011-02-17 23:27:00